I have a strange issue with -Replace
in PowerShell. This command output a list, filtered:
PS C:\> dism /image:pro /Get-ProvisionedAppxPackages | Select-String -Pattern 'Nome'
Nome visualizzato: Microsoft.BingWeather
Nome visualizzato: Microsoft.DesktopAppInstaller
Nome visualizzato: Microsoft.GetHelp
Nome visualizzato: Microsoft.Getstarted
Nome visualizzato: Microsoft.HEIFImageExtension
Nome visualizzato: Microsoft.Messaging
Nome visualizzato: Microsoft.Microsoft3DViewer
Nome visualizzato: Microsoft.MicrosoftOfficeHub
Nome visualizzato: Microsoft.MicrosoftSolitaireCollection
Nome visualizzato: Microsoft.MicrosoftStickyNotes
Nome visualizzato: Microsoft.MixedReality.Portal
Nome visualizzato: Microsoft.MSPaint
Output is quite clean (some empty lines, not show here).
When i try to add | % {$_ -Replace '[^:]+:',''}
| % {$_ -Replace '[^:]+:',''}
for replacing all following the :
character, an extra space is added to the list, at the beginning of each line :
PS C:\> dism /image:pro /Get-ProvisionedAppxPackages | Select-String -Pattern 'Nome' | % {$_ -Replace '[^:]+:',''}
Microsoft.BingWeather
Microsoft.DesktopAppInstaller
Microsoft.GetHelp
Microsoft.Getstarted
Microsoft.HEIFImageExtension
Microsoft.Messaging
Microsoft.Microsoft3DViewer
Microsoft.MicrosoftOfficeHub
Microsoft.MicrosoftSolitaireCollection
Microsoft.MicrosoftStickyNotes
Microsoft.MixedReality.Portal
Microsoft.MSPaint
Why? I can easly fix with an extra | % {$_ -Replace '\s+',''}
| % {$_ -Replace '\s+',''}
, but I'd like to understand why it's happening.
My comment as answer:
The extra space you see is the space character directly behind the colon. Your regex is not filtering that space out, so it remains as the first character in the output.
Instead of | % {$_ -Replace '[^:]+:',''}
| % {$_ -Replace '[^:]+:',''}
, use | % {$_ -replace '^.+:\s*'}
| % {$_ -replace '^.+:\s*'}
to remove everything from the start up to and including the colon AND all possible extra space characters that folow that.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.