You need to store this code in a file with the .cmd extension, and cmd.exe is the most happy with ASCII-encoded files. Embedding the file. If you need a log file that shows the results of using -WhatIf, you can follow these steps: Open Windows PowerShell. There may be an argument needed for the batch file to work. Run Start-Transcript. SysSleep is available in OS/2's (native) RexxUtil module and in Patrick McPhee's RegUtil module for 32-bits Windows. I recommend using PowerShell ISE or another capable PowerShell editor to create the PowerShell code, and then paste it into the ASCII/ANSI-encoded .cmd file in the right place (at the bottom). Encoded text file. I would run it as either a job or a process so that your file deletes do not conflict with whatever your batch file is trying to do. "Get-Credential" requires an interactive session and cannot be run as a service or as a remoted service under SCCM. When preparing PowerShell code for others to use, it’s a lot easier to wrap it up as a PowerShell script file (*.ps1) and then execute it from a batch file (*.bat). Open the encoded.txt file and you will see a base64 encoded string representation of the dll file. This simple batch file will enable a PowerShell script file (*.ps1) to execute with Administrator permissions in Windows. But it is not a batch file. You can just call PowerShell in the batch file to run a script. The result should be a file called encoded.txt in c:\AM. Line 1 just prevents the contents of the batch file from being printed to the command prompt (so it’s optional). powershell "&{.\script.ps1}" ... Insert … Take your PowerShell commands back out of the batch file, then run the following as a PowerShell script. Alternatively, you could try running a different harmless batch file from your script just to test. Embed PowerShell code in a batch file In a certain scenario, I needed a batch file (bat or cmd extension) that runs PowerShell code, and I could have only one file, so I couldn’t go with the easy way of a batch file calling PowerShell.exe with the -File parameter specifying the path to a ps1 file. Embed PowerShell code in a batch file. Embed PowerShell scripts inside batch scripts (.cmd/.bat) :: Call ps2cmd.cmd with the filename of the PowerShell script to convert ps2cmd.cmd example.ps1 :: A example.ps1.cmd will be created Example Run your command (including the -WhatIf parameter). Base64 encoded string. Converting to ASCII for the batch file Right click on the script and select "Run as Administrator" In your batch file, do something like this. Robocopy local source \\destination /E /XO /R:1 /W:1 /MAXAGE:1 robocopy local source \\destination /E … Line 3 just appends the PowerShell script filename to the script directory to get the full path to the PowerShell script file, so this is the only line you would need … After the command completes, run Stop-Transcript. Keep them in the same directory to make … The batch file should have no problems at all running from within PS, so I think maybe Matthew has nailed it here. I was using a simple batch file initially like below. GitHub Gist: instantly share code, notes, and snippets. Batchfile. I need a batch file or powershell script to copy contents of a directory to 4 seperate locations and then remove the source files and produce a log file. Line 2 gets the directory that the batch file is in. Use the Sleep command for time … It is a PowerShell command embedded in a command line. To embed the file, simply create a new variable in your script which contains the base64 string. You can run a batch file within PowerShell. Note that starting PowerShell.exe in a batch file may add an extra second to the specified delay. Note the location and file name of the transcript file, and open this file … Maybe just run 'mkdir c:\testbat'. Use the SysSleep function whenever you need a time delay in Rexx scripts.