0

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?

2 Answers 2

2

It's because - Copy.ps1 are passed as arguments.

Try to execute your new script using the following syntax :

& 'Alter_all - Copy.ps1'
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but I am not calling new script from other script. Execution is by standard double click on file.
Then a shortcut should do the trick. Simply edit the target with powershell.exe -command "& 'Alter_all - Copy.ps1'"
I have easier solution: to remove spaces :). But still not sure why PS does this on files. If consensus is that even running file and its name is passed as arguments it is what it is OK then. To me it does not make sense. So either windows GUI double click should wrap name when you click or you should not be allowed to create file name with spaces.
0

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:

    • Your system uses a custom configuration that overrides the default behavior.
    • That custom configuration is flawed, because it cannot handle file paths with embedded spaces.

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.

    • If you do want to modify the machine-level configuration, substitute 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.

    • These behaviors can easily be tweaked by editing the code below.
  • 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.