Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a powershell script that iterates through all the services listed under HKLM:\Systemn\ControlSet001\services\ looking for the ImagePath to see if it has any spaces but no quotes.

e.g.

C:\this is\very bad\but people\do it\anyways.exe

But, these may contain switches/parameters like:

C:\this is\very bad\but people\do it\anyways.exe -foo -bar -ray:JkLmOpQ -or- C:\this is\very bad\but people\do it\anyways.exe /foo /bar /ray:JkLmOpQ

It's simple enough to just wrap things without parameters in quotes,

e.g.

$foo = "`"$bar`""

However, I'd like to handle things that may have parameters properly, e.g.

"C:\this is\very bad\but people\do it\anyways.exe" -foo -bar -ray:JkLmOpQ
"C:\this is\very bad\but people\do it\anyways.exe" /foo /bar /ray:JkLmOpQ

Considering using RegEx or splitting the string on / or - but those might have edge cases I'm missing.

share|improve this question
add comment

1 Answer

I had a look at this problem and it seems that it was spaces in Program Files that caused the problem.

This a PowerShell script I created to investigate.

Clear-Host
$keys = Get-ChildItem HKLM:\System\ControlSet001\services
$items = $keys | Foreach-Object {Get-ItemProperty $_.PsPath }
ForEach ($item in $items) {
If ($item.ImagePath -match " " -and $item.ImagePath -notmatch "`"" -and $item.ImagePath -match "Program Files") {
"{0,-28} {1,-120} " -f $Item.PSChildName, $item.ImagePath 
}
}

[For my information, what problem does the space cause as I found two Nvidia images with no quotes around their path]

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.