Solved

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

Posted on 2016-10-28
8
44 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 350 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 150 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 68

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 4

Expert Comment

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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Synchronize a new Active Directory domain with an existing Office 365 tenant
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now