0

I would like to know why I cant run this PowerShell command by a batch script command like this one:

start powershell -Verb runAs -ExecutionPolicy Unrestricted -Command xxxxxx

PS1 command:

$TargetFile = "%UserProfile%\Downloads\Police.exe"
$ShortcutFile = "$env:Public\Desktop\Police.lnk"
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
$Shortcut.TargetPath = $TargetFile
$Shortcut.Save() 

Batch script command that invoke PowerShell command:

@echo off
Echo Preparing the copy process and making a shortcut for Polixe.exe...
If exist "%UserProfile%\Downloads\Police.exe" (
  esentutl /y "%UserProfile%\Downloads\Police.exe" /d "%UserProfile%\Documents\Police.exe" /o
  start powershell -Verb runAs -ExecutionPolicy Unrestricted -Command $TargetFile = "%UserProfile%\Downloads\Police.exe" ^
$ShortcutFile = "$env:Public\Desktop\Police.lnk" ^
$WScriptShell = New-Object -ComObject WScript.Shell ^
$Shortcut = $WScriptShell.CreateShortcut($ShortcutFile) ^
$Shortcut.TargetPath = $TargetFile ^
$Shortcut.Save() ^
 ) ELSE (
If not exist "%UserProfile%\Downloads\Police.exe" echo We did no find the file Police.exe, the copy/shortcut transfer did not succeed!
)
exit

I have this errors:

'$TargetFile is not recognized as an internal or external command, operable program or batch file.  '$ShortcutFile' is not recognized as an internal or external command, operable program or batch file. '$WScriptShell' is not recognized as an internal or external command, operable program or batch file. '$Shortcut' is not recognized as an internal or external command, operable program or batch file. '$Shortcut.TargetPath' is not recognized as an internal or external command, operable program or batch file. '$Shortcut.Save' is not recognized as an internal or external command, operable program or batch file.
RemusDogg
  • 1
  • 2
  • You have to escape every (unquoted) closing `)` like `^)` within the whole block (where you are using line continuation `^`) that is to be executed via PowerShell… – aschipfl Nov 25 '20 at 12:50
  • …as a follow on, you don't need to include a caret after `$Shortcut.Save(^)`. Also why not just run the batch-file elevated, instead of just the `powershell` command. In addition, you don't need to use/set an `ExecutionPolicy` for a `-Command`, only for a `-File`. – Compo Nov 25 '20 at 12:58
  • Does this answer your question? [Convert a small PS script into a long line in a .BATch file](https://stackoverflow.com/questions/36672784/convert-a-small-ps-script-into-a-long-line-in-a-batch-file) – Squashman Nov 25 '20 at 13:59

0 Answers0