Eric Bergman-Terrell's Blog

Replacing .BAT Files with PowerShell Scripts
November 19, 2006

Now that PowerShell has been released, I've sworn off .BAT files forever. I have a few .BAT files that I'm converting to PowerShell scripts.

Here's one way to quickly convert your legacy .BAT files to PowerShell scripts that you can run from Desktop shortcuts:

Step 1: Enable script execution. By default, PowerShell doesn't allow script execution. If you execute the Get-ExecutionPolicy command immediately after installing PowerShell you'll see that the execution policy is "Restricted" which means that no scripts can be run. You can enable script execution with the Set-ExecutionPolicy command. Set the execution policy to "RemoteSigned" to allow execution of unsigned local scripts, and signed scripts downloaded from the Internet. Set the execution policy to RemoteSigned as follows:

PS C:\temp> Set-ExecutionPolicy RemoteSigned

Step 2: Create a folder in which your scripts will be stored. For example, create a folder named C:\PSScripts.

Step 3: Add a script file named Example.ps1 to the C:\PSScripts folder. Note, that's a digit one at the end of the .ps1 file type. Add the following text to Example.ps1:

Write-Output "Hello"
Start-Sleep -Seconds 10

Write-Output "Goodbye"
Start-Sleep -Seconds 10

Step 4: Create a Desktop shortcut to PowerShell: Click Start / Programs / Windows PowerShell 1.0 and right-click Windows PowerShell and click Copy. Then right-click the Desktop and click Paste.

Step 5: Rename the Desktop shortcut to Example. Right-click the shortcut, click Properties, and select the Shortcut tab. In the Target text box enter the following:

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -command "& 'C:\PSScripts\Example.ps1' "

Now when you double-click the Desktop shortcut the Example.ps1 script will run.

Keywords: PowerShell, command shell, .NET, .BAT files, scripts, scripting

Reader Comments

NameCommentURLDate/Time
MansoorHi, Thanks a lot for shareing ur code for creating bat files with powershell .This was very full for my project,.... thanks once again.... -Mansoor
 November 5, 2009
Kenny JosephThanks!
 May 18, 2009

Comment on this Blog Post

Recent Posts

TitleDate
Java Programming Tip: SWT Photo Frame ProgramOctober 31, 2016
Vault 3 (Desktop) Version 1.63 ReleasedSeptember 9, 2016
"Compliance with Court Orders Act of 2016"April 9, 2016
Disable "Visual Voicemail" on Android / T-MobileJanuary 17, 2016
IPv6 HumorDecember 10, 2015
Java Programming Tip: Specify the JVM time zoneDecember 7, 2015
Node.js / Express Programming Tip: Detect and Fix Memory LeaksOctober 27, 2015