Skip to main content

How to start a PowerShell Script from a Batch File

How to start a PowerShell Script from a Batch File

In last week’s PowerShell class in Phoenix, we had a last minute question.  It involved trying to simplify the launching of a PowerShell script for users.  Having end users working with PowerShell has long been a cumbersome task.  End users like a GUI.  We can put a GUI interface on top of our code, but it is difficult to do manually or you need a third party solution.  When you build a GUI, it also takes an additional skill set that most IT Pros do not have.

We decided to go with a batch file.  Yes, I know.  Old tech but we will give it new life.  Here is our test code for this project. We saved this file as c:\ps\Test1.ps1.

Write-Host "I work!!!" -BackgroundColor DarkMagenta

Yes, I know.  Not exactly exciting.  The purpose of this is to get it to launch with a batch file.

We looked at the PowerShell.exe Command-Line Help (https://docs.microsoft.com/en-us/powershell/scripting/core-powershell/console/powershell.exe-command-line-help?view=powershell-5.1) to see how to launch PowerShell with a script from the command line at the same time.  We came up with:

PowerShell.exe –File C:\PS\Test1.ps1

We saved this command line into a batch file in the same directory as the script and was able to launch it from a desktop shortcut icon.  Right now, this is a viable option.

What about using parameters?  This is a bit more difficult.  The original objective was to do it from a DOS command prompt, but when we add parameters, the process is just as complex as doing it PowerShell if not more.  Here is our new code.

Param ($ComputerName)
Write-Host "I work!!!" -BackgroundColor DarkMagenta
Write-Host $ComputerName

Again, I know.  Real advanced.  This is what our batch file looks like now:

PowerShell.exe –File C:\PS\Test1.ps1 –ComputerName INDY-DC1
The original goal was to simplify this so the user did not have to type in PowerShell.  At this point, I would actually have the user use PowerShell and turn this script into a cmdlet in an auto-loading module.  To do this new process via batch file, here are the steps:
1.       Open Notepad
2.       Open the batch file in notepad
3.       Manually enter the computer name.
4.       Save the file
5.       Double click the desk shortcut to the batch file.

If this was a cmdlet in an auto-loading module, here is the process:
1.       Open PowerShell
2.       Type CmdletName –ComputerName INDY-DC1
That is it!




Comments

Popular posts from this blog

Adding a Comment to a GPO with PowerShell

As I'm writing this article, I'm also writing a customization for a PowerShell course I'm teaching next week in Phoenix.  This customization deals with Group Policy and PowerShell.  For those of you who attend my classes may already know this, but I sit their and try to ask the questions to myself that others may ask as I present the material.  I finished up my customization a few hours ago and then I realized that I did not add in how to put a comment on a GPO.  This is a feature that many Group Policy Administrators may not be aware of. This past summer I attended a presentation at TechEd on Group Policy.  One organization in the crowd had over 5,000 Group Policies.  In an environment like that, the comment section can be priceless.  I always like to write in the comment section why I created the policy so I know its purpose next week after I've completed 50 other tasks and can't remember what I did 5 minutes ago. In the Group Policy module for PowerShell V3, th

Return duplicate values from a collection with PowerShell

If you have a collection of objects and you want to remove any duplicate items, it is fairly simple. # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   # Remove the duplicate values. $Set1 | Select-Object -Unique 1 2 3 4 5 6 7 What if you want only the duplicate values and nothing else? # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   #Create a second collection with duplicate values removed. $Set2 = $Set1 | Select-Object -Unique   # Return only the duplicate values. ( Compare-Object -ReferenceObject $Set2 -DifferenceObject $Set1 ) . InputObject | Select-Object – Unique 1 2 This works with objects as well as numbers.  The first command creates a collection with 2 duplicates of both 1 and 2.   The second command creates another collection with the duplicates filtered out.  The Compare-Object cmdlet will first find items that are diffe

How to list all the AD LDS instances on a server

AD LDS allows you to provide directory services to applications that are free of the confines of Active Directory.  To list all the AD LDS instances on a server, follow this procedure: Log into the server in question Open a command prompt. Type dsdbutil and press Enter Type List Instances and press Enter . You will receive a list of the instance name, both the LDAP and SSL port numbers, the location of the database, and its status.