Switch to PowerShell Overdue
Move past BAT files.
November 29, 2016
Move past BAT files.
It's been timely for a decade; the Windows world just passed the decennial anniversary of the release of PowerShell 1.0. PowerShell effectively is the preferred vehicle for Windows scripting and automation, replacing CMD.EXE, itself introduced as a ported extension of the earlier COMMAND.COM which first appeared in 1981. BAT scripting has been a conventional solution for thirty-five (!) years.
As The Register just reported, though, it won't be the standard much longer: "The newly released Windows 10 Insider Preview Build 14971 for PC has made PowerShell the default command line tool ...".
What this means for you
You're ready for the shift, of course. You like PowerShell's advantages over CMD.EXE, and you recognize PowerShell's strategic importance to Microsoft. You're already competent in PowerShell scripting.
Your co-workers who haven't kept up, though, need to take this occasion to become comfortable with PowerShell. While plenty of tutorials are available, they all assume the reader is an application programmer (or perhaps system administrator) who intends to become more-or-less expert at the language. I have found nothing for the intelligent newcomer who simply needs enough familiarity with PowerShell to apply the solution he or she already has in hand. The same gap means that a sysad with years of BAT experience will end up stumbling unnecessarily when in transition to PowerShell. No one has made sure that those first few steps of PowerShell migration go smoothly.
Immediate success
Until now. This article is a recipe for turning PowerScript commands or scripts into the precise actions necessary to apply them.
First is to recognize the starting point. Other articles present a solution as one or more lines of source code, such as
Write-Host $(hostname)
or
Get-ChildItem -Recurse -Include *.pdf |Where-Object {!$_.PSIsContainer}
When working with other tools, a "solution" might be a sequence of points, clicks, keystrokes, and so on. In the PowerShell world, "solution" refers to a text.
To use such a text, select the Start button at the lower left of your screen, then type "powershell" in the "Run box", as in the illustration here.
A "Windows PowerShell" window will pop up. Enter a command--in this case, Write-Host $(hostname), followed by the Enter key. PowerShell responds with the result of its operation. For this example, the result is the name powerspec.
Success! That's the fundamental way to execute one or more PowerShell commands: open a PowerShell window, and type in the commands. Note that there is no need to install or enable or configure anything to run PowerShell in this way, unless running a computer stuck at Windows Vista or before. PowerShell 2.0 has been part of the standard Windows operating system (OS) installation since August 2009, 3.0 became available in 2012, 4.0 the following year, and 5.0 is part of the default installation for Windows Server® 2016 and Windows® 10. While hundreds of thousands of Windows Server 2003 remain in use, compatibility between different releases of PowerShell is generally quite good, and shouldn't worry you at this stage.
Command Prompt familiarity
For a second way to run a PowerShell command, return to the Start Menu and, instead of PowerShell, enter command. This time a Command Prompt window pops up
Enter powershell -command "Write-Host $(hostname)" and the server name again appears.
While that's easy enough, why ever do this, rather than use our first interactive "Windows PowerShell" method? In part, because Command Prompt windows are common in Windows system administration, and have many other roles; once one is around, it's convenient to be able to enter PowerShell in it in such a simple way. Command Prompts are so common, in fact, that one tutorial site lists six others ways to launch the command prompt window.
Apart from several minor features beyond the scope of this recipe, a second fundamental reason to practice the Command Prompt is to prepare for the third way to run a PowerShell command.
Scripting begins
Keep the Command Prompt from the last section. Also start a WordPad session, and use it to create a file, tempmy-first.ps1, with a single line of content, the now-familiar Write-Host $(hostname). Save tempmy-first.ps1 as what WordPad categorizes "Plain text document".
Next, in the Command Prompt window, enter
powershell -command "set-executionPolicy -scope CurrentUser remoteSigned"
This is a one-time operation. Do this once, and all other PowerShell scripting for the CurrentUser is enabled. For this purpose, "script" is just a sequence of one or more PowerShell commands captured in a text file.
Finally, follow up in the Command Prompt window with powershell tempmy-first.ps1. After a moment's pause, the hostname again appears.
This is a powerful result. Use any editing operations necessary to create SOME_FOLDERMY-SCRIPT.ps1 of any length or complexity, and PowerShell executes all the contents on request of powershell SOME_FOLDERMY-SCRIPT.ps1. Hundreds of thousands of freely-available PowerShell examples, scripts, and applications immediately come into reach.
Invoke a script from within PowerShell
When trying to achieve a useful result, most of the time either the first or third method proves appropriate. One convenient way to combine the advantages of these two approaches is to return to the Windows PowerShell window from the first method, and enter simply the name of a PowerShell script, such as the tempmy-first.ps1 from above. For a fourth time, the hostname appears:
Summary
While I employ at least eleven distinct ways to execute PowerShell scripts in my own work, the four presented here meet almost all the needs of those who simply want to take advantage of solutions published as PowerShell commands or scripts:
interactive PowerShell window
command-line command invocation from Command Prompt window
command-line script invocation from Command Prompt window
script invocation from interactive PowerShell window
The recipes presented here are simple enough that they are easy to follow successfully, and to generalize to larger and more useful commands and scripts. Problems that occur in elementary uses such as these generally have to do with either security policies or filenames which embed spaces, such as "Saved Gamesexample.ps1". Both these are slightly more advanced topics for another time. The aim of the four methods above is to make general application of PowerShell solutions easy and quick.
Already on top of all this? 'Certain that everyone on your team knows to write .ps1 rather than .bat? Check carefully: from what I see of datacenters, millions of ill-remembered services, scheduled tasks, and startups play crucial roles. While I expect nearly all will continue to function even with PowerShell as Windows' default command-line language, it's a great time to ready yourself for change. November is ITPro's month for focusing on "Automation"; Make sure your whole team knows how to use PowerShell.
About the Author
You May Also Like