Thursday, February 17, 2011

What can I do with windows powershell part7

Aliases


The following task-based articles provide a brief introduction to working with Windows PowerShell aliases; these tasks include such things as retrieving and remapping existing aliases as well as creating new aliases. 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 New-Alias Cmdlet

Creating a New Windows PowerShell Alias

So you say that you like Windows PowerShell, but you have problems remembering some of the cmdlet names? Well, then why not create an alias for those cmdlets, an alias that you can remember? For example, maybe you’d prefer to type show instead of Get-ChildItem. If that’s the case, then you can use the New-Alias cmdlet to map the term show to Get-ChildItem. All you have to do is call New-Alias followed by the alias name (show) and the cmdlet the alias is being mapped to (Get-ChildItem). You know, something like this:
New-Alias show Get-ChildItem
Now, what do you suppose will happen the next time you type show in the Windows PowerShell console? You got it:
Windows PowerShell
By default, aliases are not saved between Windows PowerShell sessions: each time you restart Windows PowerShell you’ll need to recreate the alias. To ensure that your new aliases survive between Windows PowerShell sessions, create a PSConfiguration folder in your Windows PowerShell profile folder. For example:
C:\Documents and Settings\gstemp\My Documents\PSConfiguration
Note. How are you supposed to know your Windows PowerShell profile folder? Just type this command:
Get-Variable profile | Format-List
In the PSConfiguration folder, create a file named Microsoft.PowerShell_profile.ps1 and add the following command to the file:
Set-Alias show Get-ChildItem
That should do the trick.
New-Alias Aliases
  • nal

Using the Import-Alias Cmdlet

Importing Saved Windows PowerShell Aliases

If you can export all your Windows PowerShell aliases using the Export-Alias cmdlet then it seems only fitting and proper that you can import those aliases using the Import-Alias cmdlet. To import a saved alias file simply call Import-Alias followed by the file path:
Import-Alias c:\scripts\alias.txt
Note that Import-Alias does not delete or overwrite any existing Windows PowerShell aliases. For example, suppose you have an alias named xyz that maps to the Get-ChildItem cmdlet. Let’s further suppose that your text file also has an alias named xyz, this one mapping to the Get-Help cmdlet. When you import the file your version of xyz will not be overwritten; instead, Windows PowerShell will inform you that an alias named xyz already exists, and leave well enough alone. Import-Alias will only import aliases (based on name) that don’t currently exist in your Windows PowerShell environment.
Import-Alias Aliases
  • ipal

Using the Get-Alias Cmdlet

Listing All Your Windows PowerShell Aliases

The Get-Alias cmdlet simply returns a list of your Windows PowerShell aliases. Type the following, and all your aliases will be displayed on-screen:
Get-Alias
If you don’t want to see all the aliases you can use the -name parameter and specify either a single alias name or use a wildcard to return a group of aliases. For example, this command returns a list of all the aliases that start with the letter F:
Get-Alias -name f*
Like we said, you’ll get back information similar to this:
CommandType     Name                            Definition
-----------     ----                            ----------
Alias           fc                              format-custom
Alias           fl                              Format-List
Alias           foreach                         foreach-object
Alias           ft                              Format-Table
Alias           fw                              Format-Wide
Get-Alias Aliases
  • gal

Using the Export-Alias Cmdlet

Saving Windows PowerShell Aliases

The Export-Alias cmdlet provides a way to export your Windows PowerShell aliases to a text file. In its simplest form you can just call Export-Alias along with the path to the text file. For example, this command exports your alias file as a comma-separated values list, saving the file as C:\Scripts\Test.txt:
Export-Alias c:\scripts\test.txt
The resulting text file will look similar to this:
# Alias File
# Exported by : kenmyer
# Date/Time : Saturday, May 06, 2006 4:12:53 PM
# Machine : TVSFRANK
"ac","Add-Content","","ReadOnly, AllScope"
"clc","Clear-Content","","ReadOnly, AllScope"
"cli","clear-item","","ReadOnly, AllScope"
Alternatively, you can export your aliases as a script file, a file that uses a series of Set-Alias cmdlets (and thus a file that can be run as a Windows PowerShell script in order to configure those aliases). To do that, tack on the -as parameter, setting the value to “script”. That command looks like this:
Export-Alias c:\scripts\test.ps1 -as "script"
And the resulting file looks like this:
# Alias File
# Exported by : kenmyer
# Date/Time : Saturday, May 06, 2006 4:14:04 PM
# Machine : TVSFRANK
set-alias -Name:"ac" -Value:"Add-Content" -Description:"" -Option:"ReadOnly, AllScope"
set-alias -Name:"clc" -Value:"Clear-Content" -Description:"" -Option:"ReadOnly, AllScope"
set-alias -Name:"cli" -Value:"clear-item" -Description:"" -Option:"ReadOnly, AllScope"
Just for the heck of it, you might also want to export a single alias or set of aliases. For example, suppose you’ve created a set of aliases, each one starting with the letter X. To export these aliases as a script (thus making it easy for someone else to configure those same aliases on their computer) use the -name parameter along with the wildcard X*:
Export-Alias c:\scripts\test.ps1 -name x*  -as "script"
By default Export-Alias will overwrite any existing files with the same name; if a file named Test.txt already exists then Export-Alias will overwrite that file. To prevent that from happening, just add the -noclobber parameter:
Export-Alias c:\scripts\test.txt -noclobber
If you run this command and Test.xml already exists you’ll get back the following error message:
Export-Clixml : File C:\scripts\test.txt already exists and NoClobber was specified.
Export-Alias Aliases
  • epal

Using the Set-Alias Cmdlet

Remapping an Existing Windows PowerShell Alias

Set-Alias allows you to map an existing alias to a different cmdlet. For example, suppose you have an alias named show that, for some reason, is mapped to the Remove-Item cmdlet. You’d prefer that show be mapped to Get-ChildItem. Well, why didn’t you say so:
Set-Alias show Get-ChildItem
Just call Set-Alias followed by the alias name, followed by the appropriate cmdlet. That’s all you have to do.
Although there’s no corresponding cmdlet set aside for removing a mapped alias, you can achieve that task by using Remove-Item:
Remove-Item alias:\show

1 comment:

  1. Are you looking to earn cash from your websites/blogs by popunder ads?
    If so, did you take a look at PopCash?

    ReplyDelete