Friday, June 8, 2012

Spicy Windows PowerShell


In today’s business world it is important to get as much done in as little time as possible. People are finding ways to automate every repetitive task for which they’re responsible. Consistency is important when automating tasks so that every administrator on the team is equally capable of stepping in to help other team members when necessary. For more information on the background and overview of PowerShell visit http://en.wikipedia.org/wiki/Windows_PowerShell.


Microsoft has designated PowerShell as part of its common engineering criteria for all server products.


PowerShell like any language consists of commands, variables, functions, flow control methods, and other features necessary to enable work to be done. Because it is an interpreted language, the scripts don’t have to be compiled into an executable form to be run.






A cmdlet (command-let) is a common-line utility built into PowerShell to provide some functionality. These cmdlets use a verb-noun naming convention, so it’s fairly easy to understand what they’re doing. Microsoft has provided about 130 built-in cmdlets with the default installation of PowerShell, and additional cmdlets are installed depending on various server products that may be running.


Cmdlets are frequently aliased. In other words, a different command can be entered to run the cmdlet, rather than using its own name. For example, when browsing a directory, the PowerShell cmdlet to view the contents of the current directory is Get-ChildItem. If you’re familiar with DOS you’d use dir. With Unix, you’d use the ls command. All three of these commands do the same operation, and both dir and ls are included with PowerShell as aliases of the Get-ChildItem cmdlet.


A feature of Unix shell scripting environments is the capability to “pipe” the results of one command into another command’s input buffer. This is a feature of shell scripting that makes it so powerful – the ability to string multiple commands together to provide information quickly. PowerShell provides this ability, but differs from Unix shell scripting in that the Unix pipe sends text from one command to another, whereas PowerShell pipes objects from one cmdlet to another. Unix scripts must parse the text from one command using commands such as grep, awk, and sed to format the text in a form the next command expects. PowerShell’s objects are understood by the receiving cmdlet and no such parsing is required.


More information on the features of PowerShell can be found at http://technet.microsoft.com/en-us/library/bb978526.aspx


For getting started with PowerShell visit


http://technet.microsoft.com/en-us/library/bb978526.aspx



Below is a comparison chart of PowerShell cmdlets with similar commands:

PowerShell (Cmdlet)
PowerShell (Alias)
Description
Get-ChildItem
gci, dir, ls
List all files / directories in the (current) directory
Get-Content
gc, type, cat
Get the content of a file
Get-Command
gcm
List available commands
Get-Help
help, man
Help on commands
Clear-Host
cls, clear
Clear the screen[Note 1]
Copy-Item
cpi, copy, cp
Copy one or several files / a whole directory tree
Move-Item
mi, move, mv
Move a file / a directory to a new location
Remove-Item
ri, del, erase, rmdir, rd, rm
rm, rmdir
Delete a file / a directory
Rename-Item
rni, ren, mv
Rename a file / a directory
Get-Location
gl, pwd
Display the current directory/present working directory.
Pop-Location
popd
popd
Change the current directory to the directory most recently pushed onto the stack
Push-Location
pushd
pushd
Push the current directory onto the stack
Set-Location
sl, cd, chdir
cd
Change the current directory
Tee-Object
tee
n/a
Pipe input to a file or variable, then pass the input along the pipeline
Write-Output
echo, write
echo
Print strings, variables etc. to standard output
Get-Process
gps, ps
tlist,[Note 2] tasklist[Note 3]
List all currently running processes
Stop-Process
spps, kill
Stop a running process
Select-String
where
Print lines matching a pattern
Set-Variable
sv, set
env, export, set, setenv
Set the value of a variable / create a variable

No comments:

Post a Comment