{"id":53,"date":"2007-01-22T08:18:14","date_gmt":"2007-01-22T16:18:14","guid":{"rendered":"http:\/\/testblog.sapien.com\/index.php\/2007\/01\/22\/out-out-damn-variable\/"},"modified":"2007-01-22T08:18:14","modified_gmt":"2007-01-22T16:18:14","slug":"out-out-damn-variable","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2007\/01\/22\/out-out-damn-variable\/","title":{"rendered":"&quot;Out, out damn variable&quot;"},"content":{"rendered":"<p>Ok, maybe I&#8217;m not Lady Macbeth dealing with a guilty conscience but there are times I have to get things out of my system, like information via PowerShell. Often I want to store the results of a cmdlet in a variable for additional processing.&nbsp; Usually I do something like this:<\/p>\n<\/p>\n<div class=\"wlWriterSmartContent\" id=\"57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:761998ea-aa02-49a3-86fc-cb4ecfc8b639\" contenteditable=\"false\" style=\"margin: 0px; padding: 0px; display: inline; float: none;\">\n<pre style=\"background-color: White;\"><div><!--Code highlighting produced by Actipro CodeHighlighter (freeware)http:\/\/www.CodeHighlighter.com\/--><span style=\"color: rgb(0, 0, 0);\">$myProcesses<\/span><span style=\"color: rgb(0, 0, 0);\">=<\/span><span style=\"color: rgb(0, 0, 255);\">get<\/span><span style=\"color: rgb(0, 0, 0);\">-<\/span><span style=\"color: rgb(0, 0, 0);\">process<\/span><\/div><\/pre>\n<\/div>\n<p>An alternative is to use one of the common paramaters, -OutVariable. With this parameter I can achieve the same result with this expression:<\/p>\n<\/p>\n<div class=\"wlWriterSmartContent\" id=\"57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:063f5c0f-8d4d-4a5d-a82b-c40d4b402f5b\" contenteditable=\"false\" style=\"margin: 0px; padding: 0px; display: inline; float: none;\">\n<pre style=\"background-color: White;\"><div><!--Code highlighting produced by Actipro CodeHighlighter (freeware)http:\/\/www.CodeHighlighter.com\/--><span style=\"color: rgb(0, 0, 255);\">get<\/span><span style=\"color: rgb(0, 0, 0);\">-<\/span><span style=\"color: rgb(0, 0, 0);\">process <\/span><span style=\"color: rgb(0, 0, 0);\">-<\/span><span style=\"color: rgb(0, 0, 0);\">outvariable myProcesses<\/span><\/div><\/pre>\n<\/div>\n<p>I can then access $myProcesses just as I normally would.&nbsp; You only need to specify the variable name (myProcesses, not the variable $myProcesses). If you don&#8217;t want to see the cmdlet output because you&#8217;re going to manipulate the result simply pipe the output to null.<\/p>\n<\/p>\n<div class=\"wlWriterSmartContent\" id=\"57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:ddf4e5a6-ca08-47d1-8af0-41a1b7e5d3b9\" contenteditable=\"false\" style=\"margin: 0px; padding: 0px; display: inline; float: none;\">\n<pre style=\"background-color: White;\"><div><!--Code highlighting produced by Actipro CodeHighlighter (freeware)http:\/\/www.CodeHighlighter.com\/--><span style=\"color: rgb(0, 0, 255);\">get<\/span><span style=\"color: rgb(0, 0, 0);\">-<\/span><span style=\"color: rgb(0, 0, 0);\">process <\/span><span style=\"color: rgb(0, 0, 0);\">-<\/span><span style=\"color: rgb(0, 0, 0);\">outvariable myProcesses <\/span><span style=\"color: rgb(0, 0, 0);\">|<\/span><span style=\"color: rgb(0, 0, 0);\"> <\/span><span style=\"color: rgb(0, 0, 255);\">out<\/span><span style=\"color: rgb(0, 0, 0);\">-<\/span><span style=\"color: rgb(0, 0, 255);\">null<\/span><\/div><\/pre>\n<\/div>\n<p>Fundamentally there&#8217;s not much difference, unless you want to streamline code like this:<\/p>\n<\/p>\n<div class=\"wlWriterSmartContent\" id=\"57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:a305920b-6b87-4e9c-b59e-1a0a11c0c0c4\" contenteditable=\"false\" style=\"margin: 0px; padding: 0px; display: inline; float: none;\">\n<pre style=\"background-color: White;\"><div><!--Code highlighting produced by Actipro CodeHighlighter (freeware)http:\/\/www.CodeHighlighter.com\/--><span style=\"color: rgb(0, 0, 255);\">get<\/span><span style=\"color: rgb(0, 0, 0);\">-<\/span><span style=\"color: rgb(0, 0, 0);\">process <\/span><span style=\"color: rgb(0, 0, 0);\">-<\/span><span style=\"color: rgb(0, 0, 0);\">outvariable myProcesses <\/span><span style=\"color: rgb(0, 0, 0);\">|<\/span><span style=\"color: rgb(0, 0, 0);\"> <\/span><span style=\"color: rgb(0, 0, 255);\">out<\/span><span style=\"color: rgb(0, 0, 0);\">-<\/span><span style=\"color: rgb(0, 0, 255);\">null<\/span><span style=\"color: rgb(0, 0, 0);\"> ;write<\/span><span style=\"color: rgb(0, 0, 0);\">-<\/span><span style=\"color: rgb(0, 0, 0);\">host $myProcesses.count processes<\/span><\/div><\/pre>\n<\/div>\n<p>This will display a message like: 65 processes<\/p>\n<p>One thing to be careful of is where you use -OutVariable.&nbsp; Try this code and look at $result.<\/p>\n<\/p>\n<div class=\"wlWriterSmartContent\" id=\"57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:7e54b557-40a9-490a-b90e-ad96b6913319\" contenteditable=\"false\" style=\"margin: 0px; padding: 0px; display: inline; float: none;\">\n<pre style=\"background-color: White;\"><div><!--Code highlighting produced by Actipro CodeHighlighter (freeware)http:\/\/www.CodeHighlighter.com\/--><span style=\"color: rgb(0, 0, 255);\">get<\/span><span style=\"color: rgb(0, 0, 0);\">-<\/span><span style=\"color: rgb(0, 0, 0);\">process <\/span><span style=\"color: rgb(0, 0, 0);\">-<\/span><span style=\"color: rgb(0, 0, 0);\">outvariable result <\/span><span style=\"color: rgb(0, 0, 0);\">|<\/span><span style=\"color: rgb(0, 0, 0);\"> Where {$_.name <\/span><span style=\"color: rgb(0, 0, 0);\">-<\/span><span style=\"color: rgb(0, 0, 0);\">eq <\/span><span style=\"color: rgb(0, 0, 0);\">\"<\/span><span style=\"color: rgb(0, 0, 0);\">svchost<\/span><span style=\"color: rgb(0, 0, 0);\">\"<\/span><span style=\"color: rgb(0, 0, 0);\">}<\/span><\/div><\/pre>\n<\/div>\n<p>On the console you&#8217;ll see something like this:<\/p>\n<\/p>\n<div class=\"wlWriterSmartContent\" id=\"57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:7f5135d8-dc29-49ba-9337-e9ec6c9d8f2c\" contenteditable=\"false\" style=\"margin: 0px; padding: 0px; display: inline; float: none;\">\n<pre style=\"background-color: White;\"><div><!--Code highlighting produced by Actipro CodeHighlighter (freeware)http:\/\/www.CodeHighlighter.com\/--><span style=\"color: rgb(0, 0, 0);\">Handles  NPM<\/span><span style=\"color: rgb(0, 0, 0);\">(<\/span><span style=\"color: rgb(0, 0, 0);\">K<\/span><span style=\"color: rgb(0, 0, 0);\">)<\/span><span style=\"color: rgb(0, 0, 0);\">    PM<\/span><span style=\"color: rgb(0, 0, 0);\">(<\/span><span style=\"color: rgb(0, 0, 0);\">K<\/span><span style=\"color: rgb(0, 0, 0);\">)<\/span><span style=\"color: rgb(0, 0, 0);\">      WS<\/span><span style=\"color: rgb(0, 0, 0);\">(<\/span><span style=\"color: rgb(0, 0, 0);\">K<\/span><span style=\"color: rgb(0, 0, 0);\">)<\/span><span style=\"color: rgb(0, 0, 0);\"> VM<\/span><span style=\"color: rgb(0, 0, 0);\">(<\/span><span style=\"color: rgb(0, 0, 0);\">M<\/span><span style=\"color: rgb(0, 0, 0);\">)<\/span><span style=\"color: rgb(0, 0, 0);\">   CPU<\/span><span style=\"color: rgb(0, 0, 0);\">(<\/span><span style=\"color: rgb(0, 0, 0);\">s<\/span><span style=\"color: rgb(0, 0, 0);\">)<\/span><span style=\"color: rgb(0, 0, 0);\">     Id ProcessName<br>-------  ------    -----      ----- -----   ------     -- -----------<br>    <\/span><span style=\"color: rgb(0, 0, 0);\">137<\/span><span style=\"color: rgb(0, 0, 0);\">       <\/span><span style=\"color: rgb(0, 0, 0);\">3<\/span><span style=\"color: rgb(0, 0, 0);\">     <\/span><span style=\"color: rgb(0, 0, 0);\">1496<\/span><span style=\"color: rgb(0, 0, 0);\">       <\/span><span style=\"color: rgb(0, 0, 0);\">3396<\/span><span style=\"color: rgb(0, 0, 0);\">    <\/span><span style=\"color: rgb(0, 0, 0);\">35<\/span><span style=\"color: rgb(0, 0, 0);\">    <\/span><span style=\"color: rgb(0, 0, 0);\">14.72<\/span><span style=\"color: rgb(0, 0, 0);\">    <\/span><span style=\"color: rgb(0, 0, 0);\">904<\/span><span style=\"color: rgb(0, 0, 0);\"> svchost<br>    <\/span><span style=\"color: rgb(0, 0, 0);\">520<\/span><span style=\"color: rgb(0, 0, 0);\">      <\/span><span style=\"color: rgb(0, 0, 0);\">14<\/span><span style=\"color: rgb(0, 0, 0);\">     <\/span><span style=\"color: rgb(0, 0, 0);\">2304<\/span><span style=\"color: rgb(0, 0, 0);\">       <\/span><span style=\"color: rgb(0, 0, 0);\">4128<\/span><span style=\"color: rgb(0, 0, 0);\">    <\/span><span style=\"color: rgb(0, 0, 0);\">38<\/span><span style=\"color: rgb(0, 0, 0);\">    <\/span><span style=\"color: rgb(0, 0, 0);\">69.20<\/span><span style=\"color: rgb(0, 0, 0);\">    <\/span><span style=\"color: rgb(0, 0, 0);\">980<\/span><span style=\"color: rgb(0, 0, 0);\"> svchost<br>   <\/span><span style=\"color: rgb(0, 0, 0);\">1878<\/span><span style=\"color: rgb(0, 0, 0);\">      <\/span><span style=\"color: rgb(0, 0, 0);\">52<\/span><span style=\"color: rgb(0, 0, 0);\">    <\/span><span style=\"color: rgb(0, 0, 0);\">34152<\/span><span style=\"color: rgb(0, 0, 0);\">      <\/span><span style=\"color: rgb(0, 0, 0);\">43756<\/span><span style=\"color: rgb(0, 0, 0);\">   <\/span><span style=\"color: rgb(0, 0, 0);\">251<\/span><span style=\"color: rgb(0, 0, 0);\">   <\/span><span style=\"color: rgb(0, 0, 0);\">862.52<\/span><span style=\"color: rgb(0, 0, 0);\">   <\/span><span style=\"color: rgb(0, 0, 0);\">1116<\/span><span style=\"color: rgb(0, 0, 0);\"> svchost<br>    <\/span><span style=\"color: rgb(0, 0, 0);\">106<\/span><span style=\"color: rgb(0, 0, 0);\">       <\/span><span style=\"color: rgb(0, 0, 0);\">6<\/span><span style=\"color: rgb(0, 0, 0);\">     <\/span><span style=\"color: rgb(0, 0, 0);\">1448<\/span><span style=\"color: rgb(0, 0, 0);\">       <\/span><span style=\"color: rgb(0, 0, 0);\">2708<\/span><span style=\"color: rgb(0, 0, 0);\">    <\/span><span style=\"color: rgb(0, 0, 0);\">29<\/span><span style=\"color: rgb(0, 0, 0);\">    <\/span><span style=\"color: rgb(0, 0, 0);\">92.58<\/span><span style=\"color: rgb(0, 0, 0);\">   <\/span><span style=\"color: rgb(0, 0, 0);\">1352<\/span><span style=\"color: rgb(0, 0, 0);\"> svchost<br>    <\/span><span style=\"color: rgb(0, 0, 0);\">162<\/span><span style=\"color: rgb(0, 0, 0);\">       <\/span><span style=\"color: rgb(0, 0, 0);\">3<\/span><span style=\"color: rgb(0, 0, 0);\">     <\/span><span style=\"color: rgb(0, 0, 0);\">3240<\/span><span style=\"color: rgb(0, 0, 0);\">       <\/span><span style=\"color: rgb(0, 0, 0);\">4036<\/span><span style=\"color: rgb(0, 0, 0);\">    <\/span><span style=\"color: rgb(0, 0, 0);\">38<\/span><span style=\"color: rgb(0, 0, 0);\">    <\/span><span style=\"color: rgb(0, 0, 0);\">22.28<\/span><span style=\"color: rgb(0, 0, 0);\">   <\/span><span style=\"color: rgb(0, 0, 0);\">1412<\/span><span style=\"color: rgb(0, 0, 0);\"> svchost<br>    <\/span><span style=\"color: rgb(0, 0, 0);\">173<\/span><span style=\"color: rgb(0, 0, 0);\">       <\/span><span style=\"color: rgb(0, 0, 0);\">5<\/span><span style=\"color: rgb(0, 0, 0);\">     <\/span><span style=\"color: rgb(0, 0, 0);\">2104<\/span><span style=\"color: rgb(0, 0, 0);\">       <\/span><span style=\"color: rgb(0, 0, 0);\">3404<\/span><span style=\"color: rgb(0, 0, 0);\">    <\/span><span style=\"color: rgb(0, 0, 0);\">36<\/span><span style=\"color: rgb(0, 0, 0);\">    <\/span><span style=\"color: rgb(0, 0, 0);\">19.10<\/span><span style=\"color: rgb(0, 0, 0);\">   <\/span><span style=\"color: rgb(0, 0, 0);\">1428<\/span><span style=\"color: rgb(0, 0, 0);\"> svchost<br>    <\/span><span style=\"color: rgb(0, 0, 0);\">106<\/span><span style=\"color: rgb(0, 0, 0);\">       <\/span><span style=\"color: rgb(0, 0, 0);\">3<\/span><span style=\"color: rgb(0, 0, 0);\">     <\/span><span style=\"color: rgb(0, 0, 0);\">1316<\/span><span style=\"color: rgb(0, 0, 0);\">       <\/span><span style=\"color: rgb(0, 0, 0);\">2952<\/span><span style=\"color: rgb(0, 0, 0);\">    <\/span><span style=\"color: rgb(0, 0, 0);\">34<\/span><span style=\"color: rgb(0, 0, 0);\">    <\/span><span style=\"color: rgb(0, 0, 0);\">54.13<\/span><span style=\"color: rgb(0, 0, 0);\">   <\/span><span style=\"color: rgb(0, 0, 0);\">2856<\/span><span style=\"color: rgb(0, 0, 0);\"> svchost<br><\/span><\/div><\/pre>\n<\/div>\n<p>But if you look at $result you&#8217;ll see it shows every process. Why? The OutVariable parameter is storing output of the Get-Process cmdlet and storing the output in $result. What you really need it to use OutVariable with the Where-object cmdlet like this:<\/p>\n<\/p>\n<div class=\"wlWriterSmartContent\" id=\"57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:ed71d18d-0188-4579-a1c6-d3000107c937\" contenteditable=\"false\" style=\"margin: 0px; padding: 0px; display: inline; float: none;\">\n<pre style=\"background-color: White;\"><div><!--Code highlighting produced by Actipro CodeHighlighter (freeware)http:\/\/www.CodeHighlighter.com\/--><span style=\"color: rgb(0, 0, 255);\">get<\/span><span style=\"color: rgb(0, 0, 0);\">-<\/span><span style=\"color: rgb(0, 0, 0);\">process <\/span><span style=\"color: rgb(0, 0, 0);\">|<\/span><span style=\"color: rgb(0, 0, 0);\"> Where {$_.name <\/span><span style=\"color: rgb(0, 0, 0);\">-<\/span><span style=\"color: rgb(0, 0, 0);\">eq <\/span><span style=\"color: rgb(0, 0, 0);\">\"<\/span><span style=\"color: rgb(0, 0, 0);\">svchost<\/span><span style=\"color: rgb(0, 0, 0);\">\"<\/span><span style=\"color: rgb(0, 0, 0);\">} <\/span><span style=\"color: rgb(0, 0, 0);\">-<\/span><span style=\"color: rgb(0, 0, 0);\">outvariable result <\/span><span style=\"color: rgb(0, 0, 0);\">|<\/span><span style=\"color: rgb(0, 0, 0);\"> <\/span><span style=\"color: rgb(0, 0, 255);\">out<\/span><span style=\"color: rgb(0, 0, 0);\">-<\/span><span style=\"color: rgb(0, 0, 255);\">null<\/span><\/div><\/pre>\n<\/div>\n<p>Now if you look at $result you&#8217;ll get the filtered results.&nbsp; Using OutVariable won&#8217;t change the world but it might make it help condense your PowerShell expressions.<\/p>\n<p>&nbsp;<\/p>\n<div class=\"wlWriterSmartContent\" id=\"0767317B-992E-4b12-91E0-4F059A8CECA8:82825021-cb62-4d7a-9365-38325112e245\" contenteditable=\"false\" style=\"margin: 0px; padding: 0px; display: inline;\">Technorati tags: <a href=\"http:\/\/technorati.com\/tags\/PowerShell\" rel=\"tag\">PowerShell<\/a>, <a href=\"http:\/\/technorati.com\/tags\/CommonParameters\" rel=\"tag\">CommonParameters<\/a>, <a href=\"http:\/\/technorati.com\/tags\/OutVariable\" rel=\"tag\">OutVariable<\/a>, <a href=\"http:\/\/technorati.com\/tags\/Scripting\" rel=\"tag\">Scripting<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Ok, maybe I&#8217;m not Lady Macbeth dealing with a guilty conscience but there are times I have to get things out of my system, like information via PowerShell. Often I want to store the results of a cmdlet in a variable for additional processing.  Usually I do something like this:$myProcesses=get-processAn alternative is to use one of the common paramaters, -OutVariable.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2,25],"tags":[],"class_list":["post-53","post","type-post","status-publish","format-standard","hentry","category-general","category-windows-powershell"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/53","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=53"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/53\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=53"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=53"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=53"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}