Reputation: 7
I am executing PS script under w2008R2 . e.g.
Alter_all.ps1
If I make standard copy of it to keep original intact next copy is made e.g.
Alter_all - Copy.ps1
If I try to execute new edited script Alter_all - Copy.ps1 it runs content from previous script no matter what. To resolve this I found that I need to rename script to be without spaces and only then script works with new values
e.g.
Alter_all-Copy.ps1
It seems that Powershell somehow checks name of script but stops at empty space name thinking that you are executing same script you were executing previously? What is the reason behind this?
Upvotes: 0
Views: 672
Reputation: 437998
Arnaud Claudel's helpful answer shows you how to invoke a script with embedded spaces in its path from a PowerShell console window or script.
In a later comment you state that you're not calling your script from the command line, but attempting to run it by double-clicking from File Explorer:
By default, for security reasons, double-clicking *.ps1
files does NOT run them - instead, they are opened for editing.
That double-clicking actually executes your scripts if they don't contains spaces in their paths implies two things:
Therefore, you need to fix your custom configuration, by editing the registry:
Note:
The following assumes the default configuration and overrides it at the current-user level, which doesn't require admin privileges.
HKEY_LOCAL_MACHINE
for HKEY_CURRENT_USER
below, as noted in the comments.The currently configured execution policy is respected, and profiles are loaded; -NoExit
is used to invoke powershell.exe
, which means that the windows stays open after the script exits.
Note how the placeholder for the script file path, %1
, is enclosed in \"...\"
to ensure that even paths with embedded spaces are passed as a single argument to powershell.exe
's -File
parameter.
# Create temp. file for registry definitions.
$tmpFile = [IO.Path]::GetTempFileName()
# Change this to 'HKEY_LOCAL_MACHINE' to override
# the machine-level configuration - REQUIRES RUNNING AS ADMIN.
$rootKey = 'HKEY_CURRENT_USER'
# Create the registry definitions...
@"
Windows Registry Editor Version 5.00
[$rootKey\Software\Classes\Microsoft.PowerShellScript.1\shell\Open\command]
@="$($PSHOME -replace '\\', '\\')\\powershell.exe -NoExit -File \"%1\" %*"
"@ > $tmpFile
# ... and import them.
reg.exe import $tmpFile
# Clean up.
Remove-Item -LiteralPath $tmpFile
Upvotes: 0
Reputation: 3138
It's because - Copy.ps1
are passed as arguments.
Try to execute your new script using the following syntax :
& 'Alter_all - Copy.ps1'
Upvotes: 2