Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-10-28
8
Medium Priority
?
161 Views
Last Modified: 2016-11-11
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"
$src="c:\temp\ugl\"+$userid
$dest="c:\temp\ugl2\"+$userid
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

0
Comment
Question by:crcsupport
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 1400 total points
ID: 41864303
Save the PowerShell script as a .ps1 file and run it in using PowerShell.exe from a batch file..
@ECHO OFF
PowerShell.exe -ExecutionPolicy Bypass -Command "& 'C:\scripts\Test.ps1'"
PAUSE

Open in new window

Or
@ECHO OFF
PowerShell.exe -ExecutionPolicy Bypass -File "C:\scripts\Test.ps1"
PAUSE

Open in new window

Ref : http://ss64.com/ps/powershell.html

PS : remove PAUSE command if you want to automatically close the batch file..
1
 
LVL 1

Author Comment

by:crcsupport
ID: 41864307
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.
0
 
LVL 1

Author Comment

by:crcsupport
ID: 41864310
Wait a minute, after I put @ECHO OFF, it recognizes Powershell. Why does this make different?
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 40

Expert Comment

by:Subsun
ID: 41864333
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:
%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\

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

Open in new window

0
 
LVL 71

Assisted Solution

by:Qlemo
Qlemo earned 600 total points
ID: 41864360
I think you have stored the  batch file as Unicode or UTF8 instead of ANSI/ASCII.
0
 
LVL 1

Author Comment

by:crcsupport
ID: 41864439
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
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 41864731
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.
2
 
LVL 6

Expert Comment

by:Kevin Stanush
ID: 41883281
fyi, BOM for Unicode is 0xFEFF.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Screencast - Getting to Know the Pipeline

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question