First PowerShell Script with Batch File

I just started experimenting with PowerShell for a project I’m doing.  I haven’t seen any good examples on the web for how to run a PowerShell script generically from a batch file, so I’m sharing this framework.

First of all, before you run PowerShell scripts, you must change the security settings so that they will run.  You can do this in PowerShell itself:

C:\>powershell
PS C:\> Get-ExecutionPolicy
Restricted
PS C:\> Set-ExecutionPolicy RemoteSigned

After you have affected the security change, you can start using PowerShell.  Next, write a batch file to execute your script:

:: psscript.bat
set psscript='C:\psscript.ps1'

echo Running PowerShell Script: %psscript%
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe ^&%psscript% %*
set psscript=

A couple of comments on the batch file:

Notice that the path to psscript.ps1 is in single quotes.

I am executing the script by passing an odd array of characters to powershell.exe.  The carat is an escape character for .bat files which tells windows not to process the “&” as if it were part of the command line.  Therefore, the batch file passes & directly to powershell.  In addition, I’m telling the batch file to pass all batch arguments by using %* (older versions of dos and maybe windows have used %1- to pass all arguments, but that definitely does not work on my testbed using Windows 2008 R2).   On the command prompt, the command would look like this:

powershell.exe &'path-to-ps1.ps1' arg1 arg2

And here is the code for my first PowerShell script.  It is designed to take a directory path as the argument from the command line and output a list of files and directories sorted in order of size from largest to smallest.

Here is the first PowerShell script.  It’s really simple, and makes use of piping.

# psscript.ps1
Get-ChildItem $args[0] | Sort-Object Length

To execute the code, run the batch file and pass in an argument like so:

psscript1.bat C:\

You should get a listing of your files and directories in C:\ ordered by size.  Enjoy!

4 Replies to “First PowerShell Script with Batch File”

  1. I am trying to write the below for a class, I have never worked with Powershell before so any help is appreciated.

    At command file startup, the color should be set to white text on a blue background and the screen should be cleared
    · The menu options should be visible until you exit the script (unless displaying an option)
    · A title for your utility should be displayed
    · The date and time should be displayed at the top of the menu directly after or under the title
    · The screen should be cleared before displaying the menu after commands are run
    The menu should include the following options:
    1. Show System Hostname
    2. Show System IP Addresses
    3. Show System DNS Servers
    4. Show System Default Gateway
    5. Show Amount of Memory Available to Run Applications
    6. Show PATH environment variable
    7. List Drivers Installed on System
    8. List current running tasks
    9. Kill task
    10. Display a text file on the screen
    11. Get File Security Information
    12. Show All TCP/IP Network Connections and listening Ports

    For items 9, 10 and 11, you must prompt the user for an entry to use in running the command.

    · You must display a prompt for the user to input an option
    · Output that runs more than the length of the command window should be paged with the user prompted to press a key to run the next screen
    · The user should be given time to examine the output of their selection before the menu is redisplayed

  2. I think your best resource for asking a question like this would be stackoverflow.com. This is a great resource for programming tips. You should try to do the program yourself, ad then ask specific questions about the things you are stuck on. Otherwise, people on the site are less inclined to answer because 1. You didn’t try it yourself first, 2. It amounts to asking for free consulting, 3. It’s harder to answer a question that is comprehensive like this than a specific and detailed question that relates to a problem you are running into. I hope all of this is helpful to you when solving this and future programming/scripting problems you are faced with.

Leave a Reply

Your email address will not be published. Required fields are marked *


*