Calling powershell cmdlets from Windows batch file

Ok something so simple is just not working for me. I got a cmdlet that accepts a single parameter. I am trying to call a cmdlet within a Windows batch file. The batch file contains:

cd %SystemRoot%\system32\WindowsPowerShell\v1.0
powershell Set-ExecutionPolicy Unrestricted
powershell 'C:\convert-utf8-to-utf16.ps1 C:\test.txt'
powershell Set-ExecutionPolicy Restricted
pause

My ps1 file again not doing anything special:

function convert-utf8-to-utf16 {   
  $tempfile = "C:\temp.txt"
  set-ExecutionPolicy Unrestricted
  get-content -Path $args[0] -encoding utf8 | out-file $tempfile -encoding Unicode
  set-ExecutionPolicy Restricted
}

When i execute the bat file it just runs to completion (no error messages) and it does not appear to create the temp.txt file.

I can run the powershell command file at the PS command prompt but not in cmd!

Anyone got any ideas what could be wrong?

Answers


Starting with Powershell version 2, you can run a Powershell script like so...

powershell -ExecutionPolicy RemoteSigned -File "C:\Path\Script.ps1" "Parameter with spaces" Parameter2

Now if I could only figure out a way to handle dragging and dropping files to a Powershell script.


I explain both why you would want to call a PowerShell script from a batch file and how to do it in my blog post here.

This is basically what you are looking for:

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& 'C:\convert-utf8-to-utf16.ps1' 'C:\test.txt'"

And if you need to run your PowerShell script as an admin, use this:

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""C:\convert-utf8-to-utf16.ps1"" ""C:\test.txt""' -Verb RunAs}"

Rather than hard-coding the entire path to the PowerShell script though, I recommend placing the batch file and PowerShell script file in the same directory, as my blog post describes.


The problem is in the ps1 file - you declare a function but you don't call it. I would modify it like this:

param($path)
function convert-utf8-to-utf16 {   
 $tempfile = "C:\temp.txt"
 set-ExecutionPolicy Unrestricted
 get-content -Path $args[0] -encoding utf8 | out-file $tempfile -encoding Unicode
 set-ExecutionPolicy Restricted
}

convert-utf8-to-utf16 $path

it will work. However, it is not needed, you can simply ommit the function declaration and move the body into the script itself:

param($path)
$tempfile = "C:\temp.txt"
set-ExecutionPolicy Unrestricted
get-content -Path $path -encoding utf8 | out-file $tempfile -encoding Unicode
set-ExecutionPolicy Restricted

# Test-Args.ps1
param($first, $second)
write-host $first
write-host $second

Call from Command Prompt:

PowerShell.exe -NoProfile -Command "& {./Test-Args.ps1 'C:\Folder A\One' 'C:\Folder B\Two'}"

What's confusing is that if the script is in a folder path containing spaces, PowerShell doesn't recognize the script name in quotes:

PowerShell.exe -NoProfile -Command "& {'C:\Folder X\Test-Args.ps1' 'C:\Folder
 A\One' 'C:\Folder B\Two'}"

But you can get around that using something like:

PowerShell.exe -NoProfile -Command "& {set-location 'C:\Folder X';./Test-Args.ps1 'C:\Folder
 A\One' 'C:\Folder B\Two'}"

Don't use spaces in your .PS1 file name, or you're outta luck.


I got this working...The ps1 file does not need to be wrapped into a function. Just this declaration is ok.

$tempfile = "C:\temp.txt"  
get-content -Path $args[0] -encoding utf8 | out-file $tempfile -encoding unicode      

and the bat file calls it like:

cd %SystemRoot%\system32\WindowsPowerShell\v1.0
powershell Set-ExecutionPolicy Unrestricted
powershell "& 'C:\convert-utf8-to-utf16.ps1 C:\test.txt' 'C:\test.txt'"
powershell Set-ExecutionPolicy Restricted
pause

Try this syntax instead:

cd %SystemRoot%\system32\WindowsPowerShell\v1.0
powershell {Set-ExecutionPolicy Unrestricted}
powershell "& C:\convert-utf8-to-utf16.ps1 C:\test.txt"
powershell {Set-ExecutionPolicy Restricted}
pause

Need Your Help

Importing Eclipse project to Android Studio not working

android eclipse android-studio

I am trying to fork GCMHelloWorld from github. When I try to Import Project (Eclipse ADT, Gradle, etc.) using Android Studio, I get an error saying There are uncoverable errors which must be correc...

SQL set floating point precision

sql floating-point precision truncate

For a SQL int that is being converted to a float, how do I set the precision of the floating point number?