Programming4us
         
 
 
Windows

Windows 7 : Scripting Windows with PowerShell - Getting Started with PowerShell

5/4/2011 6:20:02 PM
Windows 7 ships with PowerShell 2.0 installed and ready for action, so you can start scripting right away. Although that term generally means something like “entering multiple statements or commands in a file that you can then execute all at once,” things are a bit different in the PowerShell world. What I mean is that when you’re at the PowerShell command line, you can execute single commands, as you can using Command Prompt, but you can also run through an entire script by executing multiple, consecutive commands. For example, you can run one command that stores data in a variable, and then you can run another command later on that includes that variable.

So when you’re executing commands in a PowerShell session, you’re essentially always executing a script, even if it’s a script that contains only a single command.

Starting a PowerShell Session

To get PowerShell up and running, select Start, type power, and then select Windows PowerShell in the search results. Windows 7 starts a new PowerShell console session, and displays it in the Windows PowerShell window, as shown in Figure 1.

Figure 1. Windows PowerShell is eerily similar to Command Prompt.

As you can see, on the surface PowerShell looks like a Command Prompt clone with its character-based console and command-line prompt. In fact, not only does PowerShell look like Command Prompt, it also works like Command Prompt. That is, almost anything you can do in a Command Prompt session you can also do in exactly the same way in a PowerShell session:

  • PowerShell supports the standard Command Prompt commands, including cd, del, dir, echo, md, move, ren, rm, rmdir, set, sort, and start.

  • PowerShell supports the standard DOSKEY techniques for recalling command lines and running multiple commands at once.

  • You can use PowerShell to start batch files, script, and programs.

  • PowerShell supports redirection (such as >) and piping (|).

  • You can use PowerShell’s system menu (click the icon in the upper-left corner of the window) to select, copy, and paste text, just as with the Command Prompt window.

All this means that if you’re comfortable working with Command Prompt, you won’t have much of a learning curve when it comes to PowerShell (at least as far as the above techniques are concerned).

Understanding PowerShell Cmdlets

What truly separates PowerShell from Command Prompt is the concept of the cmdlet (pronounced “commandlet”), which is a PowerShell component that encapsulates some larger functionality into a single item. (If you’re familiar with Excel, a cmdlet is analogous to an Excel worksheet function.) The name of each PowerShell cmdlet takes the following general form:

verb-subject

Here, subject refers to some kind of object, and verb is some action that the cmdlet performs on that object. For example, you can use the Get-Host cmdlet to return information about the current PowerShell hosting environment, or you can use the Out-File cmdlet to output data to a file.


Although you can run most cmdlets with no other text, most of the time you’ll augment a cmdlet name with various parameters, depending on the cmdlet syntax. For example, you use the Write-Host cmdlet to write text to the hosting environment (which means the command line in a PowerShell console session), so you need to specify the text you want to write, as in this example:

write-host "Hello PowerShell World!"

I should also mention here that many cmdlets have alternate names called aliases that you can use. For example, the Get-Service cmdlet returns the current status of whatever service you specify, as in this example:

get-service w32time

However, Get-Service also uses the alias gsv, the following command is equivalent:

gsv w32time

Table 1 lists the PowerShell aliases

Table 1. Cmdlet Aliases Used By PowerShell
AliasCmdlet
%ForEach-Object
?Where-Object
acAdd-Content
asnpAdd-PSSnapIn
catGet-Content
cdSet-Location
chdirSet-Location
clcClear-Content
clearClear-Host
clhyClear-History
cliClear-Item
clpClear-ItemProperty
clsClear-Host
clvClear-Variable
compareCompare-Object
copyCopy-Item
cpCopy-Item
cpiCopy-Item
cppCopy-ItemProperty
isepowershell_ise.exe
iwmiInvoke-WMIMethod
killStop-Process
lpOut-Printer
lsGet-ChildItem
manhelp
mdmkdir
measureMeasure-Object
miMove-Item
mountNew-PSDrive
moveMove-Item
mpMove-ItemProperty
mvMove-Item
nalNew-Alias
ndrNew-PSDrive
niNew-Item
nmoNew-Module
nsnNew-PSSession
nvNew-Variable
cvpaConvert-Path
dbpDisable-PSBreakpoint
delRemove-Item
diffCompare-Object
dirGet-ChildItem
ebpEnable-PSBreakpoint
echoWrite-Output
epalExport-Alias
epcsvExport-Csv
epsnExport-PSSession
eraseRemove-Item
etsnEnter-PSSession
exsnExit-PSSession
fcFormat-Custom
flFormat-List
foreachForEach-Object
ftFormat-Table
fwFormat-Wide
galGet-Alias
gbpGet-PSBreakpoint
gcGet-Content
gciGet-ChildItem
gcmGet-Command
gcsGet-PSCallStack
gdrGet-PSDrive
ghyGet-History
ogvOut-GridView
ohOut-Host
popdPop-Location
psGet-Process
pushdPush-Location
pwdGet-Location
rInvoke-History
rbpRemove-PSBreakpoint
rcjbReceive-Job
rdRemove-Item
rdrRemove-PSDrive
renRename-Item
riRemove-Item
rjbRemove-Job
rmRemove-Item
rmdirRemove-Item
rmoRemove-Module
rniRename-Item
rnpRename-ItemProperty
rpRemove-ItemProperty
rsnRemove-PSSession
rsnpRemove-PSSnapin
rvRemove-Variable
rvpaResolve-Path
rwmiRemove-WMIObject
sajbStart-Job
giGet-Item
gjbGet-Job
glGet-Location
gmGet-Member
gmoGet-Module
gpGet-ItemProperty
gpsGet-Process
groupGroup-Object
gsnGet-PSSession
gsnpGet-PSSnapIn
gsvGet-Service
guGet-Unique
gvGet-Variable
gwmiGet-WmiObject
hGet-History
historyGet-History
icmInvoke-Command
iexInvoke-Expression
ihyInvoke-History
iiInvoke-Item
ipalImport-Alias
ipcsvImport-Csv
ipmoImport-Module
ipsnImport-PSSession
salSet-Alias
sapsStart-Process
sasvStart-Service
sbpSet-PSBreakpoint
scSet-Content
selectSelect-Object
setSet-Variable
siSet-Item
slSet-Location
sleepStart-Sleep
sortSort-Object
spSet-ItemProperty
spjbStop-Job
sppsStop-Process
spsvStop-Service
startStart-Process
svSet-Variable
swmiSet-WMIInstance
teeTee-Object
typeGet-Content
whereWhere-Object
wjbWait-Job
writeWrite-Output

Note

Notice that all the standard Command Prompt commands—including cd, del, dir, echo, md, move, ren, rm, rmdir, set, sort, and start—are actually aliases of PowerShell cmdlets (Set-Location, Remove_Item, Get-ChildItem, and so on).

Other -----------------
- Scripting Windows 7 with WSH : Programming the Windows Management Instrumentation Service
- Scripting Windows 7 with WSH : Scripting Internet Explorer
- Scripting Windows 7 with WSH : Programming the WshNetwork Object
- Scripting Windows 7 with WSH : Programming the WshShell Object (part 2)
- Scripting Windows 7 with WSH : Programming the WshShell Object (part 1)
- Scripting Windows 7 with WSH : Programming the WScript Object
- Scripting Windows 7 with WSH : Programming Objects
- Scripting Windows 7 with WSH : Scripts and Script Execution
- Adding Macs to Your Windows 7 Network : Letting Windows Computers See Your Mac Shares
- Adding Macs to Your Windows 7 Network : Using a Mac to Make a Remote Desktop Connection to Windows 7
- Adding Macs to Your Windows 7 Network : Connecting to a Windows Shared Folder
- Adding Macs to Your Windows 7 Network : Connecting to the Windows Network
- Windows 7 : Controlling and Customizing Your Website (part 5) - Viewing the Server Logs
- Windows 7 : Controlling and Customizing Your Website (part 4) - Disabling Anonymous Access
- Windows 7 : Controlling and Customizing Your Website (part 3) - Working Without a Default Document
- Windows 7 : Controlling and Customizing Your Website (part 2) - Setting the Website’s Default Document
- Windows 7 : Controlling and Customizing Your Website (part 1)
- Windows 7 : Adding Folders and Files to the Default Website (part 3) - Adding a Folder to the Default Website
- Windows 7 : Adding Folders and Files to the Default Website (part 2) - Changing the Default Website Home Page
- Windows 7 : Adding Folders and Files to the Default Website (part 1) - Setting Permissions on the Default Website Folder
 
 
Most View
- SQL Server 2012 : T-SQL Enhancements - The INSERT OVER DML Syntax (part 1) - A Filterable Alternative to OUTPUT…INTO
- Developing for Windows Phone and Xbox Live : Graphics Pipeline
- Coding JavaScript for Mobile Browsers (part 11)
- Performing SharePoint 2010 Installations (part 1) - SharePoint 2010 Standalone Installation
- Troubleshooting Windows Home Server 2011 : Troubleshooting Device Problems
- Windows Server 2008 : Monitoring System Performance (part 2)
- jQuery 1.3 : Sharing a plugin with the world
- Exchange Server 2007: Configure a Unified Messaging Server
- SQL Server 2008 : Developing Custom Managed Database Objects (part 5) - Developing Managed User-Defined Aggregates
- Executing a Stored Procedure via a Linked Server
Top 10
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 3) - Configuring Recipient Filtering
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 2)
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 1)
- Implementing Edge Services for an Exchange Server 2007 Environment : Installing and Configuring the Edge Transport Server Components
- What's New in SharePoint 2013 (part 7) - BCS
- What's New in SharePoint 2013 (part 6) - SEARCH
- What's New in SharePoint 2013 (part 6) - WEB CONTENT MANAGEMENT
- What's New in SharePoint 2013 (part 5) - ENTERPRISE CONTENT MANAGEMENT
- What's New in SharePoint 2013 (part 4) - WORKFLOWS
- What's New in SharePoint 2013 (part 3) - REMOTE EVENTS