Cannot Delete a file in system32

There are a few different ways to do it. For such a simple command it would be unusual to create a PowerShell script on the server and then download the script using the client.

One common way would be to use the ‘PowerShell’ Action Type on the task. Honestly I rarely use that and I’m not sure whether it runs the 64-bit or 32-bit PowerShell.

For a single command line, you could launch PowerShell to run the command directly. Here’s an example I have for adding a Windows Defender exclusion for the BESClient process via PoweShell. (note that curly brackets need to be escaped…sometimes…see Tip: Escaping curly brackets for substitutions in ActionScript )

action uses wow64 redirection false

waithidden powershell.exe -ExecutionPolicy Bypass Add-MpPreference -ExclusionPath '{pathname of data folder of client}\:{{ScanTrigger:OnAccess}'

The other common method is to build a PowerShell script on the fly with the ‘createfile’ or ‘appendfile’ commands and then execute PowerShell to run the script explicitly. This one uses a bit of extra logic to locate powershell.exe (instead of assuming it’s in the PATH environment variable), and saves the PowerShell output to a log file.

action uses wow64 redirection {not x64 of operating system}

delete __createfile

createfile until END_OF_FILE
// Some PowerShell script content goes here
END_OF_FILE

delete powershell.ps1
move __createfile powershell.ps1

waithidden cmd.exe /c ""{ pathname of file ((it as string) of value "Path" of key "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell" of native registry) }" -ExecutionPolicy Bypass -File powershell.ps1 > script-output.txt 2>&1"
delete powershell.ps1
continue if {exit code of action = 0}