Even though I’ve worked extensively with PowerShell for a number of years, I still occasionally find myself using the trial-and-error technique when trying to do something new.
Usually this means manually entering commands until I figure out the command syntax or series of commands that works for whatever I am trying to accomplish. Once I get to that point, the next step is to backtrack, figure out what I have done, then add those commands to a script. Of course, the real trick is avoiding introducing any additional mistakes in the process.
Believe it or not, PowerShell scripting doesn’t have to be quite so haphazard. It’s actually possible to tell PowerShell to build a script from the commands that you have already entered at the command line. There is no need to retype the commands that you have been using, nor do you have to painstakingly cut and paste commands. The only thing you have to do is tell PowerShell to create a script based on your command history. Let me show you how it’s done.
PowerShell contains a cmdlet called Get-History. When you enter this cmdlet, PowerShell displays a list of the cmdlets that have recently been used. You can see an example of this in Figure 1.
If you want to build a script based on the commands that you have manually entered into PowerShell, the only thing you have to do is dump the recorded command history into a text file.
Actually, there is one other thing that you will need to do in order to create a functional script. If you look at Figure 1 above, you will notice that when you use the Get-History cmdlet, it displays both an ID and a command line value. To turn your recently used commands into a script, you will need to get rid of the ID portion of the Get-History cmdlet’s output. You are also going to need to get rid of the column headers (the words ID and CommandLine).
Even with this requirement, though, it is possible to create a script from recently used PowerShell commands with two lines of code. Here they are:
$History = Get-History $History.CommandLine | Out-File C:\Scripts\sample.ps1
The first of these commands maps the Get-History cmdlet’s output to a variable called $History. While there are some ways that you can get to the same result without creating a variable, using the variable makes the whole process a little less complicated.
The second line of code extracts the list of commands from the $History variable and dumps them to a script file named C:\Scripts\Sample.ps1.
The one major disadvantage to using this technique (at least in its current form) is that the list of recently used commands may include commands that you don’t want to include in the script. There are a couple of ways to deal with this problem. The easiest option is to simply open the newly created script and delete any lines of code that shouldn’t be there. However, a more elegant solution is to tell PowerShell which commands to include in the script.
The Get-History command will normally list the 32 most recently used commands. However, you can tell PowerShell to include additional history items in the output if necessary. You can also tell PowerShell to shorten the list of commands. The trick to doing so is to use the Count parameter. If, for example, you are only interested in the five most recently used commands, you could use this command:
Get-History -Count 5
You can see what this looks like in Figure 2.
Another thing you can do is tell PowerShell that you are interested in a specific command from the list. Earlier, I used the Get-ChildItem command. Suppose I wanted to include any commands referencing ChildItem in the output. I could do that by using this command:
Get-History | Select-String -Pattern “ChildItem”
You can see what this technique looks like in Figure 3. While it probably won’t be all that useful for building a complete script, it can be really handy for figuring out what the command was that you used earlier, but just can’t seem to remember now that you need it again.
As you can see, PowerShell gives you all kinds of tools you can use for extracting commands from your recent history. While this might seem like a purely academic exercise, I have used the script-building technique that I shared with you many times in real life.
Read the full article here:https://redmondmag.com/articles/2020/12/17/automate-powershell-script-creation.aspx?m=1