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