Windows PowerShell Sessions
The following task-based articles provide a brief introduction to retrieving and managing Windows PowerShell sessions; these tasks include such things as retrieving the Windows PowerShell history and re-invoking a command contained within that history. As is so often the case with Windows PowerShell, the code snippets found in these articles can either be included within a Windows PowerShell script or typed directly into the Windows PowerShell console.
Using the Get-Culture Cmdlet
Listing Language and Locale Information
The Get-Culture cmdlet does only one thing: it returns language and locale information. For example, on an English language computer Get-Culture returns the following information:LCID Name DisplayName ---- ---- ----------- 1033 en-US English (United States)
Get-Culture
Using the Get-History Cmdlet
Listing the Current Windows PowerShell History
During each session Windows PowerShell keeps track of all the commands you type in the console window. Retrieving a list of those commands is as easy as calling the Get-History cmdlet:Get-History
Get-History 32 -count 32
$MaximumHistoryCount = 150
Get-History Aliases |
---|
|
Using the Invoke-History Cmdlet
Re-running a Previous Command
The Invoke-History cmdlet enables you to re-execute a Windows PowerShell command simply by specifying the ID number assigned to that command. (How can you determine the ID number assigned to a command? Use the Get-History cmdlet.) For example, suppose you issued a fairly complicated command at the beginning of your Windows PowerShell session, a command you now need to issue again. Instead of retyping the command (and running the risk of typing it incorrectly), run Get-History to determine the command ID (say, 3). Then simply call Invoke-History followed by the ID number:Invoke-History 3
Invoke-History 3;Invoke-History 4
Invoke-History Aliases |
---|
|
Using the Add-History Cmdlet
Restoring a Previously-Saved Windows PowerShell History
So you’re working in Windows PowerShell and you’ve just typed in a series of commands that perform a very useful task. Those commands are saved in the Windows PowerShell history, but now it’s time to go home. As you know, the moment you exit the Windows PowerShell console that history will be erased. In turn, that can mean only one thing: the next time you want to carry out that same task you’ll have to remember - and re-type - all those commands.Or will you? Suppose - just suppose - that before you exit PowerShell you use this command to save your current history as an XML file named C:\Scripts\My_History.xml:
Get-History | Export-Clixml "c:\scripts\my_history.xml"
Import-Clixml "c:\scripts\my_history.xml" | Add-History

Next we import the saved history file and call the Add-History cmdlet. Now take a look at the Windows PowerShell history:

It’s the next-best thing to having your own time machine!
No comments:
Post a Comment