How to Export PowerShell Command History To Make a Script

It is easy to export your PowerShell command history to make a script. However, you will need to remove unwanted items like column headers and command ID numbers.

Brien Posey

March 3, 2023

5 Min Read
hands typing on a laptop keyboard
Alamy

If you use PowerShell for long enough, sooner or later you are bound to run into a situation where things aren’t working quite the way that they should.

Last night, for instance, I was trying something new in PowerShell and spent about an hour trying various command combinations before I finally got the results that I wanted.

When you encounter these types of situations in PowerShell, it is relatively easy to export the commands that worked and turn them into a script. All you do is enter the Get-History cmdlet and then use the Out-File cmdlet to write the PowerShell command history to a text file.

For example, if you wanted to write the current command history to a file named history.txt, you could type:

Get-History | Out-File C:History.txt

While this command works, it does not produce a ready-to-use script. Instead, your text file will contain many items you will need to edit out. These items include column headers, command ID numbers, and unwanted commands. You can see an example of this in Figure 1.

Screenshot of full PowerShell command history

History Script 1

Figure 1. The full PowerShell command history has been dumped into a text file.

Decluttering the Text File

With that in mind, recall the situation that I described earlier. I had spent an hour trying to get a set of PowerShell commands to work properly. After getting the commands to work, I wanted to export the command history to create a script. However, my history also contained the commands that hadn’t worked, which would be exported, as well.

Related:How to Encrypt PowerShell Scripts

There are a couple of ways to deal with this. One option is to dump the entire command history into a file and then use Notepad or other text editors to remove what doesn’t belong. That’s a perfectly valid option.

Another option is to get PowerShell to remove most of the clutter for you. In this case, you might still have to do some minor editing, but you can get PowerShell to remove most of the clutter upfront.

As previously mentioned, three elements will typically end up in the exported command history that you will need to remove: column headers, command ID numbers, and unwanted commands. I will show you how to get rid of all three of these elements.

Remove Column Headers

Removing column headers is relatively simple. All you do is use the Format-Table command and append the -Hide parameter. If your goal is to strip the headers from the command history, write that history to a file and use this command:

Get-History | Format-Table -Hide | Out-File C:ScriptsHistory.txt

PowerShell screenshot shows the Format-Table cmdlet used to strip the column headers

History Script 2

Figure 2. You can use the Format-Table cmdlet to strip the column headers.

Remove the Command ID Numbers

Now let’s look at how to remove the command ID numbers. The ID numbers are the sequential numbers that appear before each command in the history output.

Removing the command IDs is just a matter of using the Select-Object command and specifying that you only want to select the commandline. When combined with the techniques discussed above, the command becomes the following:

Get-History | Select-Object CommandLine | Format-Table -Hide | Out-File C:ScriptsHistory.txt

As you can see in Figure 3, this command’s output looks much more like a script than the previous output did.

PowerShell screenshot shows column headers and command IDs have been removed

History Script 3

Figure 3. The column headers and command IDs have been removed, leaving only the command history.

Remove Unwanted Commands

Removing unwanted commands tends to be a bit trickier than the other techniques that I have discussed. In all honesty, there may be real-world situations in which it is easier to manually remove unwanted commands from the output file after the file has been created. Nonetheless, I wanted to show you some tricks for removing unwanted commands.

Exporting commands that are sequential

Here, the thing to remember is that PowerShell treats the command history as an array. As such, you simply need to specify which items from the array you want to include in the final output. This is where the command ID comes into play.

Suppose that you only wanted to include commands 3 through 6 in the output. To do so, you would set up a loop by typing 3..6.

The command would look like this:

3..6 | Get-History | Select-Object CommandLine | Format-Table -Hide | Out-File C:ScriptsHistory.txt

You can see this command and its output in Figure 4.

PowerShell screenshot shows the creation of text file containing only items 3, 4, 5, and 6 from the command history

History Script 4

Figure 4. I have created a text file containing only items 3, 4, 5, and 6 from the command history.

Exporting commands that are not sequential

But what if the commands that you wanted to export from the command history were not sequential? For instance, suppose that you wanted to export command numbers 3, 4, 5, 6, 8, 11, and 12. There are two tricks you will need to accomplish this.

The first trick is to create multiple ranges, separated by a plus sign. For example, 3..6 + 11..12 would give you items 3, 4, 5, 6, 11, and 12.

The second trick is that you may occasionally need to create a single value range. As you will recall, the original list of commands to export was 3, 4, 5, 6, 8, 11, and 12. The problem with this is that 3-6 is a range and 11-12 is a range, but 8 is a single number. The way that you would handle this is by treating 8 as a single value range. Hence, you would enter 8..8. As such, the full command would be:

3..6 + 8..8 + 11..12 | Get-History | Select-Object CommandLine | Format-Table -Hide | Out-File C:ScriptsHistory.txt

PowerShell screenshot

History Script 5

Figure 5. You can choose which commands to export.

About the Author

Brien Posey

Brien Posey is a bestselling technology author, a speaker, and a 20X Microsoft MVP. In addition to his ongoing work in IT, Posey has spent the last several years training as a commercial astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space.

http://brienposey.com/

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like