Trying to run powershell in a batch file. How do I do this?

I have a simple powershell script. I like to make this as a batch file so that a user just double clicks and run. It has a prompt to enter user id.

$userid= Read-Host "Enter A Username To Reset Settings"
robocopy $src $dest /MIR

Open in new window

When I run a batch file with follow, it just blinks cmd window and close.

powershell.exe  -noexit -executionpolicy ByPass -File "C:\scripts\file-replace.ps1"

Open in new window

Who is Participating?

Improve company productivity with a Business Account.Sign Up

SubsunConnect With a Mentor Commented:
Save the PowerShell script as a .ps1 file and run it in using PowerShell.exe from a batch file..
PowerShell.exe -ExecutionPolicy Bypass -Command "& 'C:\scripts\Test.ps1'"

Open in new window

PowerShell.exe -ExecutionPolicy Bypass -File "C:\scripts\Test.ps1"

Open in new window

Ref :

PS : remove PAUSE command if you want to automatically close the batch file..
crcsupportAuthor Commented:
This is what it  returns;
C:\scripts>∩╗┐powershell.exe  -noexit -executionpolicy ByPass -File "C:\scripts\file-replace.ps1"
'powershell.exe' is not recognized as an internal or external command,
operable program or batch file.
crcsupportAuthor Commented:
Wait a minute, after I put @ECHO OFF, it recognizes Powershell. Why does this make different?
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

I don't think adding @echo off would have fixed it..
'powershell.exe' is not recognized as an internal or external command,
Probably you are missing an environment variable in your computer.

Add this to your Path environment variable:

Or Try..
%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -executionpolicy ByPass -File "C:\scripts\file-replace.ps1"

Open in new window

QlemoConnect With a Mentor Batchelor, Developer and EE Topic AdvisorCommented:
I think you have stored the  batch file as Unicode or UTF8 instead of ANSI/ASCII.
crcsupportAuthor Commented:
I have no problem with ENV.
I just tested again, it's the same. I have to put @echo off so that the batch file recognizes powershell.exe.

Yes, I saved it as unicode. What makes difference saving unicode and ascii
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Unicode batch files require an additional option in cmd.exe when getting called. Depending on the convention used, and I can't recall which one applies here, the first two bytes of the file contain a special bytes sequence to identify the low-to-high bits sequence, and that looks like the special characters you've shown.

As a rule of thumb, always run cmd batches in ASCII mode.
Kevin StanushApplication DeveloperCommented:
fyi, BOM for Unicode is 0xFEFF.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.