Thursday, February 17, 2011

What can I do with windows powershell part9

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)
To retrieve this information simply type Get-Culture without any additional parameters:
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
By default Windows PowerShell keeps track of the most recent 64 commands typed in the console window; when you invoke the Get-History cmdlet only the 32 most recent of those commands are displayed (e.g., commands 33 through 64). So how do you view commands 1 through 32? In that case you tell Get-History to start with command 32 and count backwards (using the -count parameter) 32 times. Don’t worry; that sounds more complicated than it really is:
Get-History 32 -count 32
Incidentally, for any given Windows PowerShell session you can change the number of commands that Windows PowerShell keeps track of; that can be done by modifying the $MaximumHistoryCount variable. For example, would you prefer that Windows PowerShell keep track of your last 150 commands? Okey-doke:
$MaximumHistoryCount = 150
Get-History Aliases
  • ghy
  • h
  • history
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
The Invoke-History cmdlet accepts only a single ID. What if you’d like to execute two or more previous commands, in rapid-fire succession? In that case, simply issue a pair of Invoke-History commands, separating the two using a semi-colon:
Invoke-History 3;Invoke-History 4
Invoke-History Aliases
  • ihy
  • r
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"
Why bother saving your Windows PowerShell history? Here’s why. The next time you fire up Windows PowerShell you can use the Import-Clixml cmdlet to reload that XML file. Once you’ve done that you can then pass the contents of that file to the Add-History cmdlet. Lo and behold, at that point your old history (the set of commands saved in My_History.xml) will magically be restored to your current history. Here’s what the command to do that looks like:
Import-Clixml "c:\scripts\my_history.xml" | Add-History
Maybe one of those cool before and after picture sets will help. Let’s suppose we conducted a Windows PowerShell session and, at the end, exported the history file. We now start a brand-new Windows PowerShell session; because we haven’t done anything yet, the history is blank:
Windows PowerShell
Next we import the saved history file and call the Add-History cmdlet. Now take a look at the Windows PowerShell history:
Windows PowerShell
It’s the next-best thing to having your own time machine!

No comments:

Post a Comment