Windows 7 / Getting Started

PowerShell

Windows PowerShell is a task-based command-line shell and scripting language to help IT professionals and users control and automate the administration of the Windows operating system and the applications that run on Windows. Windows PowerShell requires the Microsoft .NET Framework 2.0, while the Windows PowerShell ISE requires the Microsoft .NET Framework 3.5 with Service Pack 1.

The Built-in Windows PowerShell commands are called cmdlets and enable you to manage client computers and servers, edit the registry and file system, perform WMI calls, and connect to the .NET Framework development environment. The Windows PowerShell commands can also be used to manage other Windows technologies, such as

  • Active Directory Domain Services
  • Windows BitLocker Drive Encryption
  • DHCP Server service
  • Group Policy
  • Remote Desktop Services
  • Windows Server Backup

Although PowerShell was introduced with Windows Server 2007, the Windows PowerShell included with Windows 7 is PowerShell 2.0. PowerShell 2.0 includes the following improvements of PowerShell 1.0:

  • Hundreds of new cmdlets including Get-Hotfix, Send-MailMessage, Get-ComputerRestorePoint, New-WebServiceProxy, Debug-Process, Add-Computer, Rename-Computer, Reset-ComputerMachinePassword, and Get-Random.
  • Remote management as commands can be run on one or multiple computers by establishing an interactive session from a single computer. Additionally, you can establish a session that receives remote commands from multiple computers.
  • Windows PowerShell Integrated Scripting Environment (ISE), which is a graphical user interface where you can run commands and write, edit, run, test, and debug scripts in the same window. It includes a built-in debugger, multiline editing, selective execution, syntax colors, line and column numbers, and context-sensitive Help.
  • Background jobs that run commands asynchronously and in the background while continuing to work in your session. You can run background jobs on a local or remote computer and store the results locally or remotely.
  • The Windows PowerShell debugger helps debug functions and scripts. You can set and remove breakpoints, step through code, check the values of variables, and display a call-stack trace.
  • Use Windows PowerShell modules to organize your Windows PowerShell scripts and functions into independent, self-contained units and package them to be distributed to other users. Modules can include audio files, images, Help files, and icons, and they run in a separate session to avoid name conflicts.
  • The new event infrastructure helps you create events, subscribe to system and application events, and then listen, forward, and act on events synchronously and asynchronously.

Some popular PowerShell commands are as follows:

  • Clear-Host: Clear the screen
  • Copy-Item: Copy files or a directory
  • Get-ChildItem: List all files or directories in the current directory
  • Get-Location: Show the current directory
  • Move-Item: Move a file or directory
  • Remove-Item: Delete a file or directory
  • Rename-Item: Rename a file or directory
  • Set-Location: Change the current directory
  • Write-Output: Print a string or variable onto the screen

One use for Windows 7 PowerShell is to execute a command on a target computer just as if you were sitting at the computer. To accomplish this, you perform these three steps:

  1. Establish a session.
  2. Execute any command, script, or cmdlet using the session.
  3. Delete the session.

To establish a session, click the Start button, select All Programs, select Accessories, select Windows PowerShell, and select Windows PowerShell.

To create a session with a computer name called RemotePCName, use the following:

New-PsSession -ComputerName myremotepc

If you want to log in with a different username than you are currently logged in as, you execute the following command:

New-PsSession -ComputerName RemotePCName -credential $prompt

To run a command, such as the ipconfig command at a remote computer, perform the following commands:

$mysession = New-PSSession -ComputerName RemotePCName
Invoke-Command { ipconfig } -Session $mysession

When you are done, it is always recommended to delete the session. To end the session, execute the following command:

Remove-PsSession -session $mysession
[Previous] [Contents]