{"id":6351,"date":"2008-04-20T19:59:33","date_gmt":"2008-04-20T19:59:33","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2008\/04\/20\/mindwarpingpower-cmdlets-scriptblock_parameters\/"},"modified":"2019-02-18T13:16:02","modified_gmt":"2019-02-18T20:16:02","slug":"mindwarpingpower-cmdlets-scriptblock_parameters","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/mindwarpingpower-cmdlets-scriptblock_parameters\/","title":{"rendered":"$MindWarpingPower = $Cmdlets + $ScriptBlock_Parameters"},"content":{"rendered":"<p>A while ago I blogged about the power of <a href=\"http:\/\/blogs.msdn.com\/powershell\/archive\/2006\/06\/23\/643674.aspx\">Flexible Pipeling Scriptblock parameters<\/a>.&#160; The mechanics of this are quite simple:&#160; In a pipeline environment, if you provide a SCRIPTBLOCK to a parameter which does not take a SCRIPTBLOCK or an OBJECT, the PowerShell engine assigns the current pipeline object to the variable &quot;$_&quot;, runs the scriptblock and uses whatever value it returns for the value of that parameter.&#160; <\/p>\n<p>&#160;<\/p>\n<p>Looking back at this, I don&#8217;t think we&#8217;ve done a good enough job explaining it or promulgating its use because it is incredibly powerful and I just don&#8217;t see it being used enough.&#160; Let me give a couple of simple examples of how I used it this morning.&#160; It is a technique that is well worth learning because you can do MIND-WARPING things once you figure out how to leverage it&#8217;s power.<\/p>\n<p>&#160;<\/p>\n<p>This morning I wanted to email a set of PS1 files to someone.&#160; Exchange blocks PS1 files so what I do is to rename these by added &quot;.txt&quot; to them.&#160; I&#8217;ve see number of people sharing scripts that do something similar using the following technique:<\/p>\n<p>&#160;<\/p>\n<p><font face=\"Courier New\" size=\"2\">foreach ($file in dir *.ps1) {     <br \/>&#160;&#160;&#160; $newName = $file.Name + &quot;.TXT&quot;      <br \/>&#160;&#160;&#160; copy-item -Path $file.Name -Destination $newName       <br \/>}<\/font><\/p>\n<p>&#160;<\/p>\n<p>That works but heavens, why do all that when you could just use ScriptBlock Parameters?&#160; Now when you start using scriptblocks, you might what to leverage our friend -WHATIF until you are comfortable with how it works (isn&#8217;t -WHATIF da bomb?).<\/p>\n<p><font face=\"Courier New\" size=\"2\">Dir *.ps1 | Copy-Item -Destination {$_.Name + &quot;.TXT&quot;} -Whatif<\/font><\/p>\n<p>&#160;<\/p>\n<p>On the way back, you can do this:<\/p>\n<p><font face=\"Courier New\" size=\"2\">Dir *.ps1.txt |Rename-item -NewName {$_.Name -replace &quot;.txt&quot;} -whatif<\/font><\/p>\n<p>&#160;<\/p>\n<p>Now the scriptblock can be as large as you like you can do anything you want in it so feel free to get creative.&#160; Imagine that you wanted to normalize\/serialize the names of a set of scripts.&#160; You could do it this way:<\/p>\n<p><font face=\"Courier New\" size=\"2\">$Global:x=0; dir *.ps1 |copy-Item -Destination {&quot;ScriptFile{0:000}&quot; -f $global:x++} -whatif<\/font><\/p>\n<p>Here is an fun one:<\/p>\n<p><font face=\"Courier New\" size=\"2\">dir *.ps1 |copy-Item -Destination {if ($_.length -ge 100) {$_.Name + &quot;.BIG&quot;} else {$_.Name + &quot;.SMALL&quot;}} -whatif<\/font><\/p>\n<p>&#160;<\/p>\n<p>Experiment and Enjoy!<\/p>\n<p>Jeffrey Snover [MSFT]   <br \/>Windows Management Partner Architect    <br \/>Visit the Windows PowerShell Team blog at:&#160;&#160;&#160; <a href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/a>    <br \/>Visit the Windows PowerShell ScriptCenter at:&#160; <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A while ago I blogged about the power of Flexible Pipeling Scriptblock parameters.&#160; The mechanics of this are quite simple:&#160; In a pipeline environment, if you provide a SCRIPTBLOCK to a parameter which does not take a SCRIPTBLOCK or an OBJECT, the PowerShell engine assigns the current pipeline object to the variable &quot;$_&quot;, runs the [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6351","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>A while ago I blogged about the power of Flexible Pipeling Scriptblock parameters.&#160; The mechanics of this are quite simple:&#160; In a pipeline environment, if you provide a SCRIPTBLOCK to a parameter which does not take a SCRIPTBLOCK or an OBJECT, the PowerShell engine assigns the current pipeline object to the variable &quot;$_&quot;, runs the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/6351","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=6351"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/6351\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=6351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=6351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=6351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}