Reputation: 75
I have a batch script which is enabling a lot of auditing. the folder from where i am running this script is placed on my desktop where the username that is logged in is "Doctor A"
(the path from where the command is running from is c:\user\Doctor a\Desktop\script\test.bat
).
And after running som batch commands I am trying to launch a PowerShell script with the following line:
powershell.exe -ExecutionPolicy Bypass "%~dp0\Audit_folders_and_regkeys.ps1"
When I run this command I get an error saying
The term 'C:\Users\Doctor' 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:16 + C:\Users\Doctor <<<< A\Desktop\CyperPilot_Audit_Conf_External_Network\CyperPilot_Audit_Conf_External_Network\\Audit_folders_and_regkeys.ps1 + CategoryInfo : ObjectNotFound: (C:\Users\Doctor:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
It seems like it wont go further than C:\Users\Doctor
what do I write in my batch file to fix this?
Upvotes: 1
Views: 2194
Reputation: 200293
When you run PowerShell the way you do (which is basically the same as using the parameter -Command
) the content of the double quoted string is interpreted as a PowerShell statement (or a list of PowerShell statements). What happens is basically this:
You enter the command:
powershell.exe -ExecutionPolicy Bypass "%~dp0\Audit_folders_and_regkeys.ps1"
CMD expands the positional parameter %~dp0
:
powershell.exe -ExecutionPolicy Bypass "c:\user\Doctor a\Desktop\script\Audit_folders_and_regkeys.ps1"
CMD launches powershell.exe
and passes the the command string to it (note the removed double quotes):
c:\user\Doctor a\Desktop\script\Audit_folders_and_regkeys.ps1
PowerShell sees the statement without the double quotes and tries to execute the (non-existent) command c:\user\Doctor
with the argument a\Desktop\script\Audit_folders_and_regkeys.ps1
.
The best way to deal with this issue is to use the parameter -File
, as @PetSerAl suggested in the comments:
powershell.exe -ExecutionPolicy Bypass -File "%~dp0\Audit_folders_and_regkeys.ps1"
Otherwise you'll have to put nested quotes in the command string to compensate for the ones that are removed in passing the argument:
powershell.exe -ExecutionPolicy Bypass "& '%~dp0\Audit_folders_and_regkeys.ps1'"
Note that in that case you also need to use the call operator (&
), otherwise PowerShell would simply echo the path string.
Upvotes: 2