Windows Server How-To

How To Get PowerShell to Write Scripts for You

Recall earlier scripts to save a ton of time.

Wouldn't it be cool if PowerShell could automatically write scripts so that you didn't have to? Believe it or not, it is possible. Of course the old saying that if something sounds too good to be true then it probably is, definitely applies to this situation.

Obviously, PowerShell can't just magically write a script for you. If it could, that would definitely be a cool capability, but it would also probably mean that I would be out of a job. What PowerShell can do, however, is to build a script that is based on your recent activity.

Let's suppose for a moment that you are manually performing some kind of long, tedious task by using PowerShell. Now imagine that everything works exactly the way that it is supposed to, but you are left thinking how much work was required. In that kind of situation, you can actually tell PowerShell to turn the work that you have just done into a script! That way, you will never have to manually perform the task again.

The reason why it is possible to build a script from your previous activities is that PowerShell retains a history of the commands that you have recently executed. You can view this history by typing Get-History. You can see what this looks like in Figure 1.

Figure 1. PowerShell maintains a history of the commands that you have used recently.

There are a couple of limitations that you need to be aware of. First, Get-History is tied to the current session. As such, you will not be able to retrieve the command history from a previous session (at least not by using this method).

The second limitation that you need to know about is that PowerShell only stores the 32 most recently used commands. You can increase the history length if you like. Suppose for instance that you wanted to store 100 commands. You could do so by typing this command:

$MaximumHistoryCount = 100

This modifies the $MaximumHistoryCount variable that PowerShell uses to determine how much history to store.

So how do we turn the command history into a script? There are two things that you will need to know in order to make this work. First, if you look at the previous screen capture, you will notice that each command has a line number. These line numbers are necessary for determining which commands to add to the script. The reason why we need to reference line numbers is because there is a good chance that you don't want to add the full contents of your command history to the script. If you look at Figure 2, for example, you will notice that my history now contains the Get-History command and the $MaximumHistoryCount command. We probably would not want to add either of these commands to a script that is based on command history.

Figure 2. Your history probably contains commands that you do not want to include in the script.

The other thing that you need to know is that PowerShell allows you to map a command's output to a variable.

So let's take a look at how this works. Let's suppose that we wanted to create a script based on lines 1-3 of the history displayed in the previous figure. We could get the Get-History command to display just those items by entering this command:

Get-History (1..3)

You can see what this looks like in Figure 3.

Figure 3. You can get the Get-History cmdlet to display only specific history items.

If we want to write this history to a script, then we need to assign the Get-History (1..3) command to a variable, and then we need to reference the variable in a For Each loop. We can then use the Add-Content cmdlet to write each of the history items to a file. Here is how it works:

$HistoryItem  = Get-History (1..3)
ForEach ($Item in $HistoryItem){Add-Content C:\Scripts\MyScript.ps1 $Item.CommandLine}

You can see what this looks like in Figure 4. You can also see that I have used the Get-ChildItem cmdlet to verify the existence of the script, and the Get-Content cmdlet to display the script's code.

[Click on image for larger view.] Figure 4. PowerShell has created a script based on my command history.

About the Author

Brien Posey is a 16-time Microsoft MVP with decades of IT experience. As a freelance writer, Posey has written thousands of articles and contributed to several dozen books on a wide variety of IT topics. Prior to going freelance, Posey was a CIO for a national chain of hospitals and health care facilities. He has also served as a network administrator for some of the country's largest insurance companies and for the Department of Defense at Fort Knox. In addition to his continued work in IT, Posey has spent the last several years actively training as a commercial scientist-astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space. You can follow his spaceflight training on his Web site.

Featured

  • Microsoft Hires Movial To Build Android OS for Microsoft Devices

    Microsoft has hired the Romanian operations of software engineering and design services company Movial to develop an Android-based operating system solution for the Microsoft Devices business segment.

  • Microsoft Ending Workflows for SharePoint 2010 Online Next Month

    Microsoft on Monday gave notice that it will be ending support this year for the "workflows" component of SharePoint 2010 Online, as well as deprecating that component for SharePoint 2013 Online.

  • Why Windows Phone Is Dead, But Not Completely Gone

    Don't call it a comeback (because that's not likely). But as Brien explains, there are three ways that today's smartphone market leaves the door open for Microsoft to bring Windows back to smartphones.

  • Feature Update Deferral Mix-Up in Windows 10 Version 2004 Further Explained

    Microsoft last week described the confusion it is attempting to avoid by removing the client graphical user interface (GUI)-based controls to defer Windows 10 feature updates, starting with version 2004.

comments powered by Disqus

Office 365 Watch

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.