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?

[Webinar] Streamline your web hosting managementRegister Today

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?
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

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 DeveloperCommented:
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
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.
All Courses

From novice to tech pro — start learning today.