{"id":52163,"date":"2009-10-27T00:01:00","date_gmt":"2009-10-27T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/10\/27\/hey-scripting-guy-what-are-the-new-cmdlets-in-windows-powershell-2-0\/"},"modified":"2009-10-27T00:01:00","modified_gmt":"2009-10-27T00:01:00","slug":"hey-scripting-guy-what-are-the-new-cmdlets-in-windows-powershell-2-0","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-what-are-the-new-cmdlets-in-windows-powershell-2-0\/","title":{"rendered":"Hey, Scripting Guy! What Are the New Cmdlets in Windows PowerShell 2.0?"},"content":{"rendered":"<p><!-- AddThis Button BEGIN --><a class=\"addthis_button\" href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\"><img decoding=\"async\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\"><\/a>     <!-- AddThis Button END --><\/p>\n<p class=\"Readeraidonly\">(<strong>Editor&rsquo;s note<\/strong>: Portions of today&#8217;s Hey, Scripting Guy! Blog post are excerpted from the Microsoft Press book, <i>Windows PowerShell 2.0 Best Practices<\/i> by Ed Wilson which is now <a href=\"http:\/\/bit.ly\/1NBxZ0\"><font face=\"Segoe\">available for pre-order<\/font><\/a>.)<\/p>\n<p><a title=\"#1\" name=\"#1\"><\/a><font size=\"2\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"><\/font><\/p>\n<p class=\"MsoNormal\">Hey Scripting Guy! I am curious about the new cmdlets that are available in Windows PowerShell 2.0. Do you have some sort of a list?<\/p>\n<p class=\"MsoNormal\">&#8212; RC<\/p>\n<p class=\"MsoNormal\"><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\">Hello RC, <\/p>\n<p class=\"MsoNormal\">Microsoft Scripting Guy Ed Wilson here. Things are rather mellow around here this morning, I am listening to Gran Ventura from <a href=\"http:\/\/en.wikipedia.org\/wiki\/Madama_Butterfly\"><font face=\"Segoe\">Madama Butterfly<\/font><\/a>, and sipping a cup of Earl Gray tea and reviewing the e-mail sent to the <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/a> inbox. <\/p>\n<h2>New cmdlets<\/h2>\n<p class=\"MsoNormal\">The number of cmdlets in Windows PowerShell 2.0 has nearly doubled over the number that shipped with the original product. These are core cmdlets and do not take into account the number of cmdlets that are included with Windows 7.0. Many of these cmdlets are for use with remoting, and the underlying WSMan technology. On a Windows 7 computer with Microsoft Remote Server Administration Tools (RSAT) installed, there are 456 cmdlets. On a Windows 7 computer with all modules loaded, but without RSAT installed, there are 251 cmdlets. On a Windows 7 computer without the modules loaded there are 236 cmdlets. On a computer with Windows PowerShell 1.0 installed and with no snap-ins installed, there are 129 cmdlets. If you would like to see the cmdlets that are installed on your computer, you can use the <b>Get-Command<\/b> cmdlet as seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Get-Command &ndash;commandtype cmdlet<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">If you want to see which modules are available on your computer that is running Windows PowerShell 2.0, you can use the <b>Get-Module<\/b> cmdlet as shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Get-Module -listavailable<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\"><b><\/p>\n<p>&nbsp;<\/p>\n<p><\/b><\/p>\n<p class=\"MsoNormal\"><b><font size=\"3\">WMI Cmdlets<\/p>\n<p><\/font><\/b><\/p>\n<p class=\"MsoNormal\">Some of the new cmdlets allow for easier use of Windows Management Instrumentation (WMI) to manage and configure your computer systems. These cmdlets are listed here:<\/p>\n<p class=\"NumList\"><b>Invoke-WmiMethod:<\/b> Calls WMI methods. Depending on the method you are trying to perform, you will either need to specify the path to the object, or you can just call the method directly. In the first example, we delete a share named <b>fso<\/b><i>.<\/i> To do this, we need to specify the path to the share. The name of the method we are using is the <b>delete<\/b> method. In the second example, we do not need to connect to a specific process because we are creating a process. The name of the method is <b>create<\/b>, and the argument we pass to the <b>create<\/b> method is the name of the process to create: <\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Invoke-WmiMethod -path &#8220;win32_share.name=&#8217;fso'&#8221; -Name delete<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Invoke-WmiMethod -Class win32_process -Name create -ArgumentList notepad.exe<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"NumList\"><b><br>Register-WmiEvent:<\/b> Registers for an event with the WMI eventing subsystem. To use this cmdlet, you write an event query using the WMI Query language (WQL) syntax. (For more information about event driven queries, refer to the Microsoft Press book, <a href=\"http:\/\/www.amazon.com\/gp\/product\/0735622310?ie=UTF8&amp;tag=tn-script-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0735622310\"><font face=\"Segoe\">Microsoft Windows Scripting with WMI: Self-Paced Learning Guide<\/font><\/a>.) In this example, we create an event that will tell us when a new process starts. The notification will contain the message, &ldquo;A new process has started.&rdquo; To receive this message we will need to use the <b>Get-PSEvent<\/b><i> <\/i>cmdlet. This process will be examined next week when the Scripting Guys take on WMI eventing using Windows PowerShell 2.0. <\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Register-WMIEvent -query &#8220;Select * From __InstanceCreationEvent within 3 Where TargetInstance ` ISA &#8216;Win32_Process'&#8221; -messageData &#8220;A new process has started.&#8221; -sourceIdentifier &#8220;New Process&#8221;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"NumList\"><b><br>Remove-WmiObject:<\/b> Deletes WMI classes and instances. By using the <b>Remove-WmiObject<\/b> cmdlet, we have another way to delete instances of classes. As an example, to remove a share we could use the delete method of the <b>Win32_Share<\/b> WMI class as illustrated earlier when we were examining the <b>Invoke-WmiMethod<\/b> cmdlet. But we could also perform the query to find the class, and pipeline the resulting WMI object to the <b>Remove-WmiObject<\/b> cmdlet as shown here. You may prefer this syntax for simplicity&rsquo;s sake: <\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Get-WmiObject -Class win32_share -Filter &#8220;name = &#8216;fso'&#8221; | Remove-WmiObject<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"NumList\"><b>Set-WmiInstance:<\/b> Creates or modifies instances of WMI classes. You use the <b>SetWmiInstance<\/b> cmdlet in places where you would have used the <b>SpawnInstance<\/b> method from within VBScript. Here we create a new environmental variable by using the WMI class <b>Win32_Environment<\/b>. Because the class has no methods, in order to create a new environmental variable, we need to create a new instance of the class. This is easy to do using the <b>Set-WmiInstance<\/b> cmdlet. The only tricky part is the way the <b>arguments<\/b> parameter needs to be specified: We use a hash table. Each property from the <b>Win32_Environment<\/b> WMI class that we need to assign a value to becomes a key value within the hash table. This code is shown here: <\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Set-WmiInstance -class win32_environment -arguments ` @{Name=&#8221;testvar&#8221;;VariableValue=&#8221;testvalue&#8221;;UserName=&#8221;&lt;SYSTEM&gt;&#8221;}<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\"><b><font size=\"3\">Event Log Cmdlets<\/p>\n<p><\/font><\/b><\/p>\n<p class=\"MsoNormal\">One area that has historically been a problem for network administrators is dealing with event logs. This problem became even more severe with the introduction of Windows Vista and the new types of event logs as well as keeping up with the sheer numbers of the logs. A number of the new cmdlets are designed to address these concerns. <\/p>\n<p class=\"NumList\"><b>Clear-EventLog:<\/b> Deletes all entries from specified event logs on the local or remote computers. In this example, we clear the contents of the application log on the local computer: <\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Clear-EventLog -LogName application<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"NumList\"><b><br>Get-Event:<\/b> Gets events from event logs and event tracing log files on local and remote computers. This cmdlet runs only on Windows Vista and later versions of Windows. As seen in the following image, Windows Vista and later versions of Windows have many diagnostic logs: <\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of Windows diagnostic logs\" alt=\"Image of Windows diagnostic logs\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/october\/hey1027\/hsg-10-27-09-01.jpg\" width=\"600\" height=\"432\"><\/p>\n<p class=\"MsoNormal\"><br>The actual names for these diagnostic logs can be rather long; the name of the log in the previous image is &#8220;Microsoft-Windows-Bits-Client\/operational.&#8221; The easiest way to refer to the logs is by using a wildcard character as seen here: <\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Get-Event -LogName *bits*<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"Readeraidonly\">As a best practice, I recommend that you use the <b>Get-EventLog<\/b><i> <\/i>cmdlet when accessing the classic event logs. Use <b>Get-Event<\/b> when working with the newer diagnostic and tracing logs. <\/p>\n<p class=\"NumList\"><b>Limit-EventLog:<\/b> Sets the event log properties that limit the size of the event log and the age of its entries on a local or remote computer. To set the retention policy for a computer, use the <b>retention<\/b> parameter as seen here: <\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Limit-EventLog -LogName application -Retention 8<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"NumList\"><b><br>New-EventLog:<\/b> Creates a new event log and a new event source on a local or remote computer. To create a new event log, you need to specify both a name and a source for the log. If you leave out the <b>computername<\/b>, it is assumed to be local. In this example, we create a new event log named <b>forScripting<\/b> with a named source called <b>ScriptErrors<\/b>. This log will be created on a remote computer named <b>berlin<\/b>. The custom event log created by this command is seen in the next image. <\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">New-EventLog -LogName forScripting -Source ScriptErrors -ComputerName berlin<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of custom event log\" alt=\"Image of custom event log\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/october\/hey1027\/hsg-10-27-09-02.jpg\" width=\"600\" height=\"432\"><a href=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/october\/hey1027\/hsg-10-27-09-02.jpg\"><\/a><\/p>\n<p class=\"MsoNormal\"><b><br>Remove-EventLog:<\/b> Deletes an event log or unregisters an event source on a local or remote computer. To remove an event log, use the<i> <\/i><b>Remove-EventLog<\/b> cmdlet to specify the log name and the computer name if applicable. This code is shown here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Remove-EventLog -LogName forscripting &ndash;computername berlin<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"NumList\"><b><br>Show-EventLog:<\/b> Displays the event logs of the local or a remote computer in Event Viewer. This command is the same as typing <span>eventvwr<\/span> inside the Windows PowerShell console. To use the <b>Show-EventLog <\/b>cmdlet, you type the name of the cmdlet and specify the name of the computer whose event logs you wish to display in the Event Viewer console. This is illustrated here: <\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Show-EventLog<span>&nbsp; <\/span>-ComputerName berlin<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"NumList\"><b><br>Write-EventLog:<\/b> Writes an event to an event log:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Write-EventLog -LogName forScripting -EventId 1000 -Source ScriptErrors `<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">-EntryType information -Message &#8220;Script Completed&#8221; &ndash;ComputerName berlin<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\"><br>RC, we hope you enjoyed this quick overview of some of the new cmdlets that are available in Windows PowerShell 2.0. Tomorrow we will continue our overview of Windows PowerShell 2.0.<\/p>\n<p class=\"MsoNormal\">If you want to know exactly what we will be looking at tomorrow, follow us on <a href=\"http:\/\/www.twitter.com\/scriptingguys\/\" target=\"_blank\"><font face=\"Segoe\">Twitter<\/font><\/a> or <a href=\"http:\/\/www.facebook.com\/group.php?gid=5901799452\" target=\"_blank\">Facebook<\/a>. If you have any questions, send e-mail to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/a> or post them on the <a href=\"http:\/\/social.technet.microsoft.com\/Forums\/en\/ITCG\/threads\/\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, keep on scripting!<\/p>\n<p><b><span>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/span><\/b><\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>(Editor&rsquo;s note: Portions of today&#8217;s Hey, Scripting Guy! Blog post are excerpted from the Microsoft Press book, Windows PowerShell 2.0 Best Practices by Ed Wilson which is now available for pre-order.) Hey Scripting Guy! I am curious about the new cmdlets that are available in Windows PowerShell 2.0. Do you have some sort of a [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,45],"class_list":["post-52163","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>(Editor&rsquo;s note: Portions of today&#8217;s Hey, Scripting Guy! Blog post are excerpted from the Microsoft Press book, Windows PowerShell 2.0 Best Practices by Ed Wilson which is now available for pre-order.) Hey Scripting Guy! I am curious about the new cmdlets that are available in Windows PowerShell 2.0. Do you have some sort of a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/52163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=52163"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/52163\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=52163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=52163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=52163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}