Powershell – Calling powershell cmdlets from Windows batch file

batch-filepowershell

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?

Best Solution

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.