- 1 year ago
Split pattern output by spaces in Powershell
To split the output of a PowerShell command by spaces, you can use the -split
operator along with the space character as the delimiter. Here's an example:
powershell$output = Get-Command | Select-Object -First 5 | ForEach-Object { $_.Name }
$splitOutput = $output -split ' '
foreach ($item in $splitOutput) {
Write-Output $item
}
In the above example, the Get-Command
cmdlet is used to retrieve a list of commands, and the Select-Object
cmdlet is used to select the first 5 command names. The output is then piped to the ForEach-Object
cmdlet to extract the name property.
The $output
variable contains the output as a single string. Using the -split
operator, we split the output by spaces (' '
) and store the result in the $splitOutput
variable.
Finally, we iterate over each item in the $splitOutput
array and print it using the Write-Output
cmdlet.
You can replace the Get-Command
part with your specific command or use any other command that produces a string output. Adjust the delimiter and processing logic based on your requirements.
I hope this helps!