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?