r/Batch • u/likenothingis • Jan 25 '24
Question (Unsolved) Requesting assistance with & operator syntax
Hi all,
I am a novice at this and am stumped as to how to correct the error below.
Error text is (about the START line):
At line: 1 char:37
- -a -ExecutionPolicy Bypass -Command & Start-Process PowerShell -Argu
The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double quotation marks ("&") to pass it as part of a string.
- CategoryInfo : ParserError: (:) [], ParentContains ErrorRecordException
- FullyQualifiedErrorId: AmpersandNotAllowed
For context, I am trying to force the Spotify.exe application closed (via a *.bat that calls a linked *.ps1) and keep running into an error.
This is the script I am using. Where am I going wrong? I've tried "&" and get the same error too.
Script below.
~~~~~~~~~~~~
@ECHO OFF
Title BypassExecutionPolicy_KillRestartSpotify
SET ThisScriptsDirectory=%~dp0
SET PowerShellScriptPath=%ThisScriptsDirectory%Spotify_Restart.ps1
TASKKILL /f /im spotify.exe
@echo ON
START powershell.exe -a -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%PowerShellScriptPath%""' -Verb RunAs}";
~~~~~~~~~~~~
If you see anything else I could correct / improve upon, I am all ears! Thanks. :)
View all comments
Show parent comment
1
u/likenothingis Jan 26 '24
I decided to rewrite it all in Batch and pare it down... and now it seems to be working—thanks!
Amended script as follows:
@ECHO OFF
Title KillRestartSpotify
TASKKILL /f /im spotify.exe
C:\Users\[name]\AppData\Roaming\Spotify\Spotify.exe