1

I'm experiencing a very strange behavior of PowerShell on my PC. I'm trying to run a script (doesn't matter what's in the script) from a path with a space in it. For example, this one:

Import-Module activedirectory
$User1 = Read-Host "Enter User to copy Group-Memberships from"
$User2 = Read-Host "Enter User to past Group-Memberships to"
Get-ADUser -Identity $User1 -Properties memberof | Select-Object -ExpandProperty memberof | Add-ADGroupMember -Members $User2 -PassThru | Select-Object -Property SamAccountName

When I'm running it from within ISE or from a previously opened PowerShell Windows it's working fine. But it immediately crashes if I'm trying to just double-click the .ps1-File. It opens a PowerShell-Windows, displays an Error-Message I had to capture with a screen recording tool because it's gone only a fraction of a second later. The Error-Message is saying:

C:\Users###\Desktop\Member : The term 'C:\Users###\Desktop\Member' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ C:\Users###\Desktop\Member of\Copy_AD_Member-of_DomainLocal.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Users###\Desktop\Member:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

So as you can see, the Path to the script is "C:\Users###\Desktop\Member of\Copy_AD_Member-of_DomainLocal.ps1", with a single space in the scripts parant directory "Member of".

As soon as I replace the space with a dash it's working fine.

My target originally was to make the script runnable even if you move it to another directory and without typing any line of code manually as a user. Is it possible to do this somehow?

Geco Mynx
  • 25
  • 8
  • 1
    Why don't you use the [call operator (&)](https://ss64.com/ps/call.html) instead of double-clicking and running it. Will save a lot of fuss! – Vivek Kumar Singh Jun 22 '18 at 12:06
  • Yeah, but I'll have to put the call operator in a script to, soo... – Geco Mynx Jun 22 '18 at 12:14
  • I kinda don't get the issue here, cause I was able to run scripts with a space in the path on another PC before, this is making me crazy – Geco Mynx Jun 22 '18 at 12:20
  • 1
    See [this](https://stackoverflow.com/questions/10137146/is-there-any-way-to-make-powershell-script-work-by-double-clicking-ps1-file) where one of the answer suggests that `one of PowerShell's security features is that users can NOT launch script with a double click`. You can try one from the proposed solutions. – Vivek Kumar Singh Jun 22 '18 at 12:28
  • Okay :o This "security feature" is a very new one then, is it? It's annoying... – Geco Mynx Jun 22 '18 at 13:06
  • I don't think so. Historically ps1 have never had double click associations in the registry. This is to prevent proliferation of malware. vbs was the same. What I suspect is that you enabled this (on purpose or not) with a .reg file or something akin to that with a plugin. It's not implemented correctly to account for space. So, if you want this to work, you have to edit that value and quote it.. `.... '%1'` or something like that. I think Viveks linked post would help point you. – Matt Jun 22 '18 at 13:08
  • No it was already there. See `Get-Help about_Command_Precedence` in your PowerShell console for more info! – Vivek Kumar Singh Jun 22 '18 at 13:11
  • Hm okay... So the only "easy to use"-way to make a .ps1-File runnable via double-click is to make sure the path doesn't have any spaces in it OR using a shortcut, where I have to manually edit the path to the script anyway, is that correct? – Geco Mynx Jun 22 '18 at 13:20
  • And something like this in the shortcut's target is not possible, is it? C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -command "& '.\Get_AD_Member-of.ps1'" – Geco Mynx Jun 22 '18 at 13:25
  • My target originally was to make the script runnable even if you move it to another directory and without typing any line of code manually as a user. – Geco Mynx Jun 22 '18 at 13:27
  • *So the only "easy to use"-way to make a .ps1-File runnable via double-click is to make sure the path doesn't have any spaces in it OR using a shortcut, where I have to manually edit the path to the script anyway, is that correct?* No. What gave you that idea? Make sure that the parameter for the file argument is properly quoted (`"%1"`) in the file association in the registry. – Ansgar Wiechers Jun 23 '18 at 23:10
  • Yeah, but that's no "easy to use"-way if you have to make sure that this is Setup for every PC in the company... – Geco Mynx Jun 25 '18 at 08:07
  • Yes, it is. It's one registry setting that can easily be deployed via group policies. – Ansgar Wiechers Jun 25 '18 at 11:34

0 Answers0