{"id":16701,"date":"2010-10-26T00:01:00","date_gmt":"2010-10-26T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/10\/26\/learn-how-to-use-net-framework-commands-inside-windows-powershell\/"},"modified":"2010-10-26T00:01:00","modified_gmt":"2010-10-26T00:01:00","slug":"learn-how-to-use-net-framework-commands-inside-windows-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/learn-how-to-use-net-framework-commands-inside-windows-powershell\/","title":{"rendered":"Learn How to Use .NET Framework Commands inside Windows PowerShell"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p><b>Summary:<\/b> Microsoft Scripting Guy Ed Wilson shows how to use .NET Framework commands inside Windows PowerShell<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\" \/>Hey, Scripting Guy! I understand that Windows PowerShell is built upon the .NET Framework. I also get the idea that some of the Windows PowerShell cmdlets are simply calling things from the .NET Framework in the background. But what good is that to me? Why do I care?<\/p>\n<p>&#8212; HB<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\" \/>Hello HB, Microsoft Scripting Guy Ed Wilson here.<\/p>\n<p><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/10\/25\/use-net-framework-classes-to-explore-windows-powershell-processes.aspx\">Yesterday<\/a> we looked at the fundamental concepts of the .NET Framework, and explored the <b>System.Diagnostics.Process<\/b> .NET Framework class. <\/p>\n<p>By using the static <b>GetProcesses<\/b> method, a listing of all the processes and their top properties is obtained. When you call a .NET Framework class, the <b>System<\/b> part of the namespace can be left off as it is assumed. Therefore, with the <b>System.Diagnostics.Process<\/b> class, <b>System.Diagnostics<\/b> is the namespace, and <b>Process<\/b> is the actual class name. When I leave the <b>System<\/b> portion off the namespace name the command morphs to the one shown here.<\/p>\n<blockquote><p><span style=\"font-family: courier new,courier\">[diagnostics.process]::GetProcesses()<\/span><\/p><\/blockquote>\n<p>The output from this command is seen in the figure below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/8182.HSG-10-26-10-01.JPG\" border=\"0\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>The output shown in this figure is very extensive and provides a useful overview of the way the system is performing. It is so useful the Windows PowerShell team has incorporated it into the <b>Get-Process<\/b> cmdlet. To use the <b>Get-Process<\/b> cmdlet you just have to type the command on Windows PowerShell console command line as seen here.<\/p>\n<blockquote><p><span style=\"font-family: courier new,courier\">Get-Process<\/span><\/p><\/blockquote>\n<p>The output from the <b>Get-Process<\/b> command is shown in the following figure.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/2475.HSG-10-26-10-02.JPG\" border=\"0\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>The output from the <b>Get-Process<\/b> cmdlet and the <b>[diagnostics.process]::GetProcesses()<\/b> are equivalent. The difference is that <b>Get-Process<\/b> alphabetizes the output by process name and <b>[diagnostics.process]::GetProcesses()<\/b> does not. <\/p>\n<p>The point of this exercise is to show that Windows PowerShell is a .NET Framework application. You do not have to type <b>[diagnostics.process]::GetProcesses()<\/b> because that is the essence of Get-Process. In fact, if I pipeline <b>Get-Process<\/b> to the <b>Get-Member<\/b> cmdlet, I can see that the <b>System.Diagnostics.Process<\/b> class is returned by the command. The command to do this is shown here.<\/p>\n<blockquote><p><span style=\"font-family: courier new,courier\">Get-Process | Get-Member<\/span><\/p><\/blockquote>\n<p>As we saw yesterday, there are an awful lot of methods and properties exposed by <b>System.Diagnostics.Process<\/b>. The output is shown in the figure below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4336.HSG-10-26-10-03.jpg\" border=\"0\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Using the .NET Framework <b>Process<\/b> class, from the <b>System.Diagnostics<\/b> namespace, I can use the static method <b>GetProcessByName<\/b>. If I am not certain how to use the method, I can pipeline the class to <b>Get-Member<\/b>, specify I want to see the static method <b>getProcessbyname<\/b> and output the returned information to the <b>format-list<\/b> cmdlet. When I do this, the output seen here appears. <\/p>\n<blockquote><p><span style=\"font-family: courier new,courier\">PS C:\\&gt; [diagnostics.process] | gm -s getProcessesbyname | fl *<\/p>\n<p>TypeName&nbsp;&nbsp; : System.Diagnostics.Process<br \/>Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : GetProcessesByName<br \/>MemberType : Method<br \/>Definition : static System.Diagnostics.Process[] GetProcessesByName(string processName), static Sys<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tem.Diagnostics.Process[] GetProcessesByName(string processName, string machineName)<\/p>\n<p>PS C:\\&gt;<\/span><\/p><\/blockquote>\n<p>The output seen earlier tells me that I have to provide either a process name or a process name and a computer name to the method. By using that information, I can return information about the explorer process. This is seen here.<\/p>\n<blockquote><p><span style=\"font-family: courier new,courier\">PS C:\\&gt; [diagnostics.process]::GetProcessesByName(&#8220;explorer&#8221;)<\/p>\n<p>Handles&nbsp; NPM(K)&nbsp;&nbsp;&nbsp; PM(K)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WS(K) VM(M)&nbsp;&nbsp; CPU(s)&nbsp;&nbsp;&nbsp;&nbsp; Id ProcessName<br \/>&#8212;&#8212;-&nbsp; &#8212;&#8212;&nbsp;&nbsp;&nbsp; &#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8211; &#8212;&#8211;&nbsp;&nbsp; &#8212;&#8212;&nbsp;&nbsp;&nbsp;&nbsp; &#8212; &#8212;&#8212;&#8212;&#8211;<br \/>&nbsp;&nbsp; 1084&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 80&nbsp;&nbsp;&nbsp; 99332&nbsp;&nbsp;&nbsp;&nbsp; 120468&nbsp;&nbsp; 447&nbsp;&nbsp;&nbsp; 13.90&nbsp;&nbsp; 3620 explorer<\/p>\n<p>PS C:\\&gt;<\/span><\/p><\/blockquote>\n<p>We see the same subset of information that was returned by the <b>Get-Process<\/b> Windows PowerShell cmdlet. We know now, that additional information would be available &#8230; the information such as seen in the previous figure. <\/p>\n<p>Remember back when we were looking at the member information for the <b>GetProcessesByName<\/b> method? There was an alternative way of calling the method that would accept both a process name and a computer name as parameters. If I wanted to use the <b>process<\/b> .NET Framework class to retrieve process information from a remote computer back in the Windows PowerShell days, this was the way to do it. (Of course, there was also the <b>Win32_Process<\/b> WMI class that was available also). To use the process class and specify a computer name, use the syntax seen here (The output is shown under the command).<\/p>\n<blockquote><p><span style=\"font-family: courier new,courier\">PS C:\\&gt; [diagnostics.process]::GetProcessesByName(&#8220;explorer&#8221;, &#8220;localhost&#8221;)<\/p>\n<p>Handles&nbsp; NPM(K)&nbsp;&nbsp;&nbsp; PM(K)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WS(K) VM(M)&nbsp;&nbsp; CPU(s)&nbsp;&nbsp;&nbsp;&nbsp; Id ProcessName<br \/>&#8212;&#8212;-&nbsp; &#8212;&#8212;&nbsp;&nbsp;&nbsp; &#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8211; &#8212;&#8211;&nbsp;&nbsp; &#8212;&#8212;&nbsp;&nbsp;&nbsp;&nbsp; &#8212; &#8212;&#8212;&#8212;&#8211;<br \/>&nbsp;&nbsp; 1488&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 96 &nbsp;&nbsp;141128&nbsp;&nbsp;&nbsp;&nbsp; 153208&nbsp;&nbsp; 589&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3620 explorer<\/span><\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p>In Windows PowerShell 2.0 the <b>Get-Process<\/b> cmdlet was upgraded to accept a <i>computername<\/i> parameter. This command and associated output is shown here.<\/p>\n<blockquote><p><span style=\"font-family: courier new,courier\">PS C:\\&gt; Get-Process -Name explorer -ComputerName localhost<\/p>\n<p>Handles&nbsp; NPM(K)&nbsp;&nbsp;&nbsp; PM(K)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WS(K) VM(M)&nbsp;&nbsp; CPU(s)&nbsp;&nbsp;&nbsp;&nbsp; Id ProcessName<br \/>&#8212;&#8212;-&nbsp; &#8212;&#8212;&nbsp;&nbsp;&nbsp; &#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8211; &#8212;&#8211;&nbsp;&nbsp; &#8212;&#8212;&nbsp;&nbsp;&nbsp;&nbsp; &#8212; &#8212;&#8212;&#8212;&#8211;<br \/>&nbsp;&nbsp; 1478&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 95&nbsp;&nbsp; 140644&nbsp;&nbsp;&nbsp;&nbsp; 152968&nbsp;&nbsp; 587&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3620 explorer<\/p>\n<p>PS C:\\&gt;<\/span><\/p><\/blockquote>\n<p>By comparing the output from the two previous commands, you can see that the output is virtually the same. This shows that the .NET Framework class <b>GetProcessByName<\/b><i> <\/i>method is behind the name parameter that is used by the <b>Get-Process<\/b> cmdlet. <\/p>\n<p>Not all the methods and properties exposed through the <b>System.Diagnostics.Process<\/b> .NET Framework class are exposed through a switch or a parameter of the <b>Get-Process<\/b> Windows PowerShell cmdlet. This is where knowing a bit about the underlying .NET Framework classes comes into play. For example, if I want to change the processor affinity of a particular process that is running on a computer, all I have to do is to use <b>Get-Process<\/b> to retrieve the process, and then assign a new value to the <b>ProcessorAffinity<\/b> property. This is illustrated here where I first launch a new instance of notepad. I then use <b>Get-Process<\/b> to retrieve the notepad process to make sure it is working correctly. After confirming that it works, I again use the <b>Get-Process<\/b> Windows PowerShell cmdlet to retrieve the notepad process, and I store it in the <i>$a<\/i> variable. I then query the <b>ProcessorAffinity<\/b> property and see that the notepad process has a processor affinity of 3. <\/p>\n<blockquote><p><span style=\"font-family: courier new,courier\">PS C:\\&gt; notepad<br \/>PS C:\\&gt; Get-Process notepad<\/p>\n<p>Handles&nbsp; NPM(K)&nbsp;&nbsp;&nbsp; PM(K)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WS(K) VM(M)&nbsp;&nbsp; CPU(s)&nbsp;&nbsp;&nbsp;&nbsp; Id ProcessName<br \/>&#8212;&#8212;-&nbsp; &#8212;&#8212;&nbsp;&nbsp;&nbsp; &#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8211; &#8212;&#8211;&nbsp;&nbsp; &#8212;&#8212;&nbsp;&nbsp;&nbsp;&nbsp; &#8212; &#8212;&#8212;&#8212;&#8211;<br \/>&nbsp;&nbsp;&nbsp;&nbsp; 67&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 7&nbsp;&nbsp;&nbsp;&nbsp; 1524&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 5416&nbsp;&nbsp;&nbsp; 71&nbsp;&nbsp;&nbsp;&nbsp; 0.03&nbsp;&nbsp; 6236 notepad<\/p>\n<p>PS C:\\&gt; $a = Get-Process notepad<br \/>PS C:\\&gt; $a.ProcessorAffinity<br \/>3<br \/>PS C:\\&gt; $a.ProcessorAffinity = 0x0002<br \/>PS C:\\&gt; $a.ProcessorAffinity<br \/>2<\/span><\/p><\/blockquote>\n<p>HB, that is all there is to using the .NET Framework <b>Process<\/b> class. Exploring .NET Framework week will continue tomorrow when we will talk about how to work with the .NET Framework classes. <\/p>\n<p>I invite you to follow me on <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a target=\"_blank\" href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a> or post them on the <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p>&nbsp;<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Summary: Microsoft Scripting Guy Ed Wilson shows how to use .NET Framework commands inside Windows PowerShell &nbsp; Hey, Scripting Guy! I understand that Windows PowerShell is built upon the .NET Framework. I also get the idea that some of the Windows PowerShell cmdlets are simply calling things from the .NET Framework in the background. [&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":[66,51,31,87,3,4,45],"class_list":["post-16701","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-net-framework","tag-getting-started","tag-operating-system","tag-processes","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; Summary: Microsoft Scripting Guy Ed Wilson shows how to use .NET Framework commands inside Windows PowerShell &nbsp; Hey, Scripting Guy! I understand that Windows PowerShell is built upon the .NET Framework. I also get the idea that some of the Windows PowerShell cmdlets are simply calling things from the .NET Framework in the background. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/16701","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=16701"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/16701\/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=16701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=16701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=16701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}