Part 1 - What's New In PowerShell V2: Computer Cmdlets

PowerShell V2 contains a lot of new cmdlets and has added many more useful parameters to existing cmdlets. In this series, we look at all the new things you can now do with PowerShell V2.

How do I know which PS Version I run?

It's pretty easy to find out whether PowerShell is installed or not: simply press WIN+R and try to run powershell.exe. If PowerShell is installed, the next thing you probably want to know is which version you are running. The best thing is to check the automatic variable $psversiontable which is present only in V2 and tells you the exact version.

Alexander has created a function called Get-PSVersion for that (http://powershell.com/cs/media/p/2617.aspx), and we also have a Power Tip for this (http://powershell.com/cs/blogs/tips/archive/2009/09/11/which-powershell-version-am-i-running.aspx).

Should you find that you are still using PowerShell V1, you may want to upgrade to V2. V2 is the default version on Windows 7 and Windows Server 2008 R2 but is also available down to Windows XP. Currently, Microsoft has made available PowerShell Version 2 RC for these older platforms (avoid PowerShell V2 CTP3 as this version is outdated by now and still had some limitations with remoting).

Eleven New Computer Cmdlets

PowerShell V2 contains eleven new cmdlets with the noun Computer (or related to it):

PS> Get-Command -Noun *computer* | Format-Table Name, PSSnapin

Name                             PSSnapIn
----                             --------
Add-Computer                     Microsoft.PowerShell.Management
Checkpoint-Computer              Microsoft.PowerShell.Management
Disable-ComputerRestore          Microsoft.PowerShell.Management
Enable-ComputerRestore           Microsoft.PowerShell.Management
Get-ComputerRestorePoint         Microsoft.PowerShell.Management
Remove-Computer                  Microsoft.PowerShell.Management
Reset-ComputerMachinePassword    Microsoft.PowerShell.Management
Restart-Computer                 Microsoft.PowerShell.Management
Restore-Computer                 Microsoft.PowerShell.Management
Stop-Computer                    Microsoft.PowerShell.Management
Test-ComputerSecureChannel       Microsoft.PowerShell.Management 

With these cmdlets, you can basically do three things: manage domain join, restart and shutting down machines and manage system restore points.

Domain And Workgroup Join And Unjoin

If you ever wanted to automatically join computers to a new domain or a peer-to-peer-workgroup, you can now do so using Add-Computer. To make the change happen, append Restart-Computer which is generally a great way of restarting machines:

add-computer -domainname Domain01; restart-computer

add-computer -workgroupname WORKGROUP-A

Add-Computer always works on the local computer. You cannot use it to join a remote computer. To get more examples, use this:

Get-Help Add-Computer -examples

To remove a computer from a domain or workgroup, use Remove-Computer. And if you'd like to reset the computer password, use Reset-ComputerMachinePassword. You can even diagnose and repair the secure channel used by a domain-joined client and his domain. Test-ComputerSecureChannel will tell you if everything is fine, and when you add the switch parameter -repair, the channel is automatically repaired.

Restarting And Shutting Down Machines

Restart-Computer allows you to restart one or more machines, locally and remote. To simply restart your local machine, use this:

restart-computer

To restart a bunch of computers remotely and also your own machine, use the -computerName parameter and a comma-delimited list:

restart-computer -computername Server01, Server02, localhost

To shut down a machine (turn it off), use Stop-Computer instead of Restart-Computer. It works the same way.

If you want to log off users (rather than restarting or stopping machines), use Get-WMIObject Win32_OperatingSystem and take advantage of its Win32Shutdown() method which also works locally and remotely.

Managing Restore Points

Restore points are system snapshots designed to conserve some system state so in case something bad happens, you can easily recover. PowerShell now allows you to manage restore points through cmdlets. Note that managing restore points requires Admin privileges.

To list all available restore points created earlier, use Get-ComputerRestorePoint:

PS> Get-ComputerRestorePoint

CreationTime           Description                    SequenceNumber    EventType         RestorePointType
------------           -----------                    --------------    ---------         ----------------
03.07.2009 05:21:46    Windows Update                 18                BEGIN_SYSTEM_C... 18
09.07.2009 20:51:12    Installiert TIPCI              21                BEGIN_SYSTEM_C... APPLICATION_I...
10.07.2009 08:51:09    Windows Update                 22                BEGIN_SYSTEM_C... 18
10.07.2009 12:16:03    Snagit 9.1.2 wird installiert  23                BEGIN_SYSTEM_C... APPLICATION_I...
14.07.2009 09:34:59    Windows Update                 24                BEGIN_SYSTEM_C... 18
16.07.2009 17:51:09    Windows Update                 25                BEGIN_SYSTEM_C... 18
17.07.2009 01:11:21    Installed Microsoft Office ... 26                BEGIN_SYSTEM_C... APPLICATION_I...
17.07.2009 10:27:10    Installed Idera PowerShellP... 28                BEGIN_SYSTEM_C... APPLICATION_I...
18.07.2009 11:40:42    Windows Update                 29                BEGIN_SYSTEM_C... 18
19.07.2009 00:42:36    Windows Update                 30                BEGIN_SYSTEM_C... 18
19.07.2009 00:48:06    Windows Modules Installer      31                BEGIN_SYSTEM_C... APPLICATION_I...
22.09.2009 13:48:14    Installed PowerShell Commun... 60                BEGIN_SYSTEM_C... APPLICATION_I...
27.09.2009 22:47:32    Installed SystemScripter       64                BEGIN_SYSTEM_C... APPLICATION_I...

The list will show all available restore points. SequenceNumber is the unique identifier for any given restore point, and you can use this id to selectively access an individual restore point. RestorePointType tells you who has created the restore point, and the switch parameter -lastStatus tells you the result of the last restore point operation.

To manually create a new restore point, for example at the beginning of a critical system change, use Checkpoint-Computer. If you'd like to restore the system to a previous restore point, use Restore-Computer and specify the restore point you want to use. To change the scope of your restore points, use Enable-ComputerRestore and Disable-ComputerRestore. These cmdlets manage which drives should be included or excluded in your snapshots. And if you'd like to quickly check results with the GUI, call rstrui.exe.

What's Next?

All of the new cmdlets covered here live in the Microsoft.PowerShell.Management Snapin. This snapin isn't new, it was already part of PowerShell V1. While in PowerShell V1 it contained 47 cmdlets, it now delivers a solid 92 cmdlets - 45 new cmdlets! We looked at 11 new cmdlets, leaving 34 new cmdlets to check out in the next parts. Hang in!

Cheers

Tobias

 


Posted Sep 28 2009, 04:03 AM by Tobias
Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.