Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.


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,


$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

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.

$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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .