By using this site, you agree to the Privacy Policy.
Accept
EnterinITEnterinITEnterinIT
  • HOME
  • IT PRO
  • TECH
  • MICROSOFT
    • Azure
    • ConfigMgr/SCCM
    • DPM
    • Orchestrator
    • Hyper-V
    • Microsoft Edge Insider
    • MSI
    • Office 365
    • Office Insider
    • Power BI
    • Skype
    • SQL Server
    • Teams
  • WINDOWS
    • Windows Admin Center
    • Windows Client
    • Windows Server
  • SCCM Query Collection List
Reading: PowerShell extract strings from a file
Font ResizerAa
EnterinITEnterinIT
Font ResizerAa
  • HOME
  • IT PRO
  • TECH
  • MICROSOFT
  • WINDOWS
  • SCCM Query Collection List
Search
  • HOME
  • IT PRO
  • TECH
  • MICROSOFT
    • Azure
    • ConfigMgr/SCCM
    • DPM
    • Orchestrator
    • Hyper-V
    • Microsoft Edge Insider
    • MSI
    • Office 365
    • Office Insider
    • Power BI
    • Skype
    • SQL Server
    • Teams
  • WINDOWS
    • Windows Admin Center
    • Windows Client
    • Windows Server
  • SCCM Query Collection List
PowerShell

PowerShell extract strings from a file

Published: February 18, 2024
1 Min Read
SHARE

PowerShell extract strings from a file.

To extract only strings from a file using PowerShell, you can use the Select-String cmdlet.

Here’s an example command to extract strings from a file:

Get-Content path/to/file.txt | Select-String -Pattern '\b\w+\b'

In this example, path/to/file.txt is the path to the file you want to extract strings from. The Get-Content cmdlet reads the file and outputs its content as an array of strings. The Select-String cmdlet then searches for strings that match the regular expression pattern \b\w+\b, which matches any word character (letters, digits, and underscores) surrounded by word boundaries. The output of the Select-String cmdlet is a collection of MatchInfo objects that contain the matching strings.

If you want to output just the matching strings (without the MatchInfo objects), you can pipe the output to the ForEach-Object cmdlet and use the $_.Matches.Value property to extract the matching strings, like this:

Get-Content path/to/file.txt | Select-String -Pattern '\b\w+\b' | ForEach-Object { $_.Matches.Value }

This will output only the matching strings from the file.

TAGGED:PowerShellWindowsWindows 10Windows Server 2016Windows Server 2019Windows Terminal
Previous Article Configuration Manager Query Collection List Configuration Manager Query Collection List
Next Article Command Line Tips: Checking TLS/SSL Certificate Expiration Dates
Leave a Comment Leave a Comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Translation

English中文(简体)DanskNederlandsFrançaisDeutschItaliano한국어PolskiPortuguêsRomânăEspañolУкраїнська
by Transposh - translation plugin for wordpress

Popular Posts

System Requirements for Windows Server 2016
Windows Server
Configuration Manager Query Collection List
Configuration Manager Query Collection List
ConfigMgr
Structured/Managed Navigation enabled on Modern Pages in Classic Team Sites
Tech
SCCM Failed to get DP locations as the expected version from MP
ConfigMgr

Recent Posts

Installing and Configuring Fail2ban for SSH Protection on Ubuntu 24.04
Linux
Enabling and Configuring FirewallD on AlmaLinux
Linux
User Creation and SSH Key Setup in AlmaLinux
Linux
How to reset password on AlmaLinux
Linux

© 2023 EnterinIT

Go to mobile version
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?