<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ScriptLore &#187; PowerShell</title>
	<atom:link href="http://scriptlore.com/category/powershell/feed/" rel="self" type="application/rss+xml" />
	<link>http://scriptlore.com</link>
	<description>All men die, not all men really script</description>
	<lastBuildDate>Mon, 22 Aug 2011 14:51:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Debug, Parameter</title>
		<link>http://scriptlore.com/powershell/debug/</link>
		<comments>http://scriptlore.com/powershell/debug/#comments</comments>
		<pubDate>Tue, 30 Sep 2008 13:48:58 +0000</pubDate>
		<dc:creator>pogenstad</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Basics]]></category>
		<category><![CDATA[Parameters]]></category>

		<guid isPermaLink="false">http://scriptlore.com/?p=16</guid>
		<description><![CDATA[This article is eagerly awaiting to be written (honestly)!]]></description>
			<content:encoded><![CDATA[<p><img class="left" src="http://scriptlore.com/images/sx/2008/powershell-parameter-debug.jpg" alt="PowerShell Debug Parameter" /></p>
<p>This article is eagerly awaiting to be written (honestly)!<br />
<span id="more-16"></span></p>
]]></content:encoded>
			<wfw:commentRss>http://scriptlore.com/powershell/debug/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Verbose, Parameter</title>
		<link>http://scriptlore.com/powershell/verbose/</link>
		<comments>http://scriptlore.com/powershell/verbose/#comments</comments>
		<pubDate>Mon, 28 Jul 2008 05:53:17 +0000</pubDate>
		<dc:creator>pogenstad</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Basics]]></category>
		<category><![CDATA[Parameters]]></category>

		<guid isPermaLink="false">http://scriptlore.com/?p=39</guid>
		<description><![CDATA[Some cmdlets can provide verbose data. This is disabled by default but can be enabled by using the -Verbose parameter. This can be useful if you want to log an action or troubleshoot your scripts. To view an example of this we will look at the Set-Alias cmdlet: PS C:\&#62; Set-Alias Time Get-Date -Verbose VERBOSE: [...]]]></description>
			<content:encoded><![CDATA[<p><img class="left" src="http://scriptlore.com/images/sx/2008/powershell-parameter-verbose.jpg" alt="PowerShell Verbose Parameter" /></p>
<p>Some cmdlets can provide verbose data. This is disabled by default but can be enabled by using the -Verbose parameter. This can be useful if you want to log an action or troubleshoot your scripts.<br />
<span id="more-39"></span><br />
To view an example of this we will look at the Set-Alias cmdlet:</p>
<p><code>PS C:\&gt; Set-Alias Time Get-Date -Verbose<br />
VERBOSE: Performing operation "Set Alias" on Target "Name: Time Value: Get-Date".<br />
PS C:\&gt;</code></p>
<p>As I said some cmdlets doesn&#8217;t provide verbose information so if we use the parameter with the Get-Date cmdlet it doesn&#8217;t provide any additional verbose data:</p>
<p><code>PS C:\&gt; Get-Date -Verbose</code></p>
<p><code>Monday, July 28, 2008 7:48:33 AM<br />
PS C:\&gt;</code></p>
<p>Generally you will find that the Verbose parameter works on PowerShell cmdlets that make some kind of change to the system.</p>
]]></content:encoded>
			<wfw:commentRss>http://scriptlore.com/powershell/verbose/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OutBuffer, Parameter</title>
		<link>http://scriptlore.com/powershell/outbuffer/</link>
		<comments>http://scriptlore.com/powershell/outbuffer/#comments</comments>
		<pubDate>Mon, 28 Jul 2008 05:16:27 +0000</pubDate>
		<dc:creator>pogenstad</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Basics]]></category>
		<category><![CDATA[Parameters]]></category>

		<guid isPermaLink="false">http://scriptlore.com/?p=20</guid>
		<description><![CDATA[The OutBuffer parameter determines how many objects will be buffered before calling the next cmdlet in the pipeline. Instead of typing out -OutBuffer you can use -ob for short. I will add more information to this page when I&#8217;ve used OutBuffer more and can provide some examples.]]></description>
			<content:encoded><![CDATA[<p><img class="left" src="http://scriptlore.com/images/sx/2008/powershell-parameter-outbuffer.jpg" alt="PowerShell OutBuffer Parameter" /></p>
<p>The OutBuffer parameter determines how many objects will be buffered before calling the next cmdlet in the pipeline.</p>
<p>Instead of typing out -OutBuffer you can use -ob for short.<br />
<span id="more-20"></span><br />
I will add more information to this page when I&#8217;ve used OutBuffer more and can provide some examples.</p>
]]></content:encoded>
			<wfw:commentRss>http://scriptlore.com/powershell/outbuffer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ErrorAction, Parameter</title>
		<link>http://scriptlore.com/powershell/erroraction/</link>
		<comments>http://scriptlore.com/powershell/erroraction/#comments</comments>
		<pubDate>Mon, 09 Jun 2008 09:07:48 +0000</pubDate>
		<dc:creator>pogenstad</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Basics]]></category>
		<category><![CDATA[Parameters]]></category>

		<guid isPermaLink="false">http://scriptlore.com/?p=17</guid>
		<description><![CDATA[<p><img class="left" src="http://scriptlore.com/images/sx/2008/powershell-parameter-erroraction.jpg" alt="PowerShell ErrorAction Parameter" /></p>
<p>Like many other aspects of PowerShell, -ErrorAction kind of explains itself when you think of it. Basically it instructs PowerShell what action to take when an error occurs. The actions you can set are Continue (which is default), Stop, SilentlyContinue and Inquire. Let&#8217;s take a look.</p>
<h3>Continue</h3>
<p>You can verify that Continue is the default action by looking at the $ErrorActionPreference variable. We&#8217;ll be using the Stop-Process Cmdlet to test this.</p>

<p><code>PS C:\&#62; $ErrorActionPreference<br />
Continue<br />
PS C:\&#62; Stop-Process 206,304<br />
Stop-Process : Cannot find a process with the process identifier 206.<br />
At line:1 char:13<br />
+ Stop-Process  &#60;&#60;&#60;&#60; 206,304<br />

Stop-Process : Cannot find a process with the process identifier 304.<br />
At line:1 char:13<br />
+ Stop-Process  &#60;&#60;&#60;</code></p>
<p>Since we know continue is the default error action we don&#8217;t need to use &#8220;-ErrorAction Continue&#8221;.</p>
<h3>Stop</h3>
<p>The Stop error action instructs PowerShell to stop executing after the first error.</p>
<p><code>PS C:\&#62; Stop-Process 206,304 -ErrorAction Stop<br />

Stop-Process : Command execution stopped because the shell variable "ErrorActionPreference" is set to Stop: Cannot find<br />
 a process with the process identifier 206.<br />
At line:1 char:13<br />
+ Stop-Process  &#60;&#60;&#60; </code></p>
<p>Compared to when we used &#8220;Continue&#8221;, PowerShell will only try to stop the process with id 206 and never move on to 304 since it failed to stop the first one.</p>
<h3>SilentlyContinue</h3>
<p>As you might guess SilentlyContinue acts like Continue with the exception that we don&#8217;t see the errors. To save some typing we don&#8217;t need to write out ErrorAction, we can use EA for short.</p>

<p><code>PS C:\&#62; Stop-Process 206,304 -EA SilentlyContinue<br />
PS C:\&#62;</code></p>
<h3>Inquire</h3>
<p>Inquire informs us that there has been an error and lets us decide what to do.</p>
<p><code>PS C:\&#62; Stop-Process 206,304 -EA Inquire</code></p>
<p><code>Confirm<br />
Cannot find a process with the process identifier 206.<br />

[Y] Yes  [A] Yes to All  [H] Halt Command  [S] Suspend  [?] Help (default is &#34;Y&#34;): h<br />
Stop-Process : Command execution stopped because the user selected the Halt option.<br />
At line:1 char:13<br />
+ Stop-Process  &#60;&#60;&#60;</code></p>]]></description>
			<content:encoded><![CDATA[<p><img class="left" src="http://scriptlore.com/images/sx/2008/powershell-parameter-erroraction.jpg" alt="PowerShell ErrorAction Parameter" /></p>
<p>Like many other aspects of PowerShell, -ErrorAction kind of explains itself when you think of it. Basically it instructs PowerShell what action to take when an error occurs. The actions you can set are Continue (which is default), Stop, SilentlyContinue and Inquire. Let&#8217;s take a look.<span id="more-17"></span></p>
<h3>Continue</h3>
<p>You can verify that Continue is the default action by looking at the $ErrorActionPreference variable. We&#8217;ll be using the Stop-Process Cmdlet to test this.</p>
<p><code>PS C:\&gt; $ErrorActionPreference<br />
Continue<br />
PS C:\&gt; Stop-Process 206,304<br />
Stop-Process : Cannot find a process with the process identifier 206.<br />
At line:1 char:13<br />
+ Stop-Process  &lt;&lt;&lt;&lt; 206,304<br />
Stop-Process : Cannot find a process with the process identifier 304.<br />
At line:1 char:13<br />
+ Stop-Process  &lt;&lt;&lt;</code></p>
<p>Since we know continue is the default error action we don&#8217;t need to use &#8220;-ErrorAction Continue&#8221;.</p>
<h3>Stop</h3>
<p>The Stop error action instructs PowerShell to stop executing after the first error.</p>
<p><code>PS C:\&gt; Stop-Process 206,304 -ErrorAction Stop<br />
Stop-Process : Command execution stopped because the shell variable "ErrorActionPreference" is set to Stop: Cannot find<br />
 a process with the process identifier 206.<br />
At line:1 char:13<br />
+ Stop-Process  &lt;&lt;&lt; </code></p>
<p>Compared to when we used &#8220;Continue&#8221;, PowerShell will only try to stop the process with id 206 and never move on to 304 since it failed to stop the first one.</p>
<h3>SilentlyContinue</h3>
<p>As you might guess SilentlyContinue acts like Continue with the exception that we don&#8217;t see the errors. To save some typing we don&#8217;t need to write out ErrorAction, we can use EA for short.</p>
<p><code>PS C:\&gt; Stop-Process 206,304 -EA SilentlyContinue<br />
PS C:\&gt;</code></p>
<h3>Inquire</h3>
<p>Inquire informs us that there has been an error and lets us decide what to do.</p>
<p><code>PS C:\&gt; Stop-Process 206,304 -EA Inquire</code></p>
<p><code>Confirm<br />
Cannot find a process with the process identifier 206.<br />
[Y] Yes  [A] Yes to All  [H] Halt Command  [S] Suspend  [?] Help (default is &quot;Y&quot;): h<br />
Stop-Process : Command execution stopped because the user selected the Halt option.<br />
At line:1 char:13<br />
+ Stop-Process  &lt;&lt;&lt;</code></p>
]]></content:encoded>
			<wfw:commentRss>http://scriptlore.com/powershell/erroraction/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WhatIf, Parameter</title>
		<link>http://scriptlore.com/powershell/whatif/</link>
		<comments>http://scriptlore.com/powershell/whatif/#comments</comments>
		<pubDate>Mon, 26 May 2008 15:59:24 +0000</pubDate>
		<dc:creator>pogenstad</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Basics]]></category>
		<category><![CDATA[Parameters]]></category>

		<guid isPermaLink="false">http://scriptlore.com/?p=21</guid>
		<description><![CDATA[<p><img class="left" src="http://scriptlore.com/images/sx/2008/powershell-parameter-whatif.jpg" alt="PowerShell WhatIf Parameter" /></p>
<p>What if you just deleted 5000 accounts from your active directory domain by accident, what if there had been an easy way to make sure you were doing the right thing before the deletion. Would you have wanted to use it? I know I would. Just imagine you were planning on deleting all accounts which had been disabled for six months but you forgot some important switch and most of your accounts were gone because of that stupid switch. Ok, so this might be a bit far fetched but these things can happen and if you are not 100 percent sure of what you are doing, it&#8217;s great to be able to see what the results of your actions will be before you take them.</p>
<p>Let&#8217;s say we&#8217;ve been taking random notes and just want to kill all notepad processes without having to choose if we want to save every single file. Working with the Get-Process, Where-Object and Stop-Process cmdlets we decide to kill all the processes which has &#8220;note&#8221; in them.</p>

<p><code>PS C:\&#62; Get-Process &#124; Where-Object {$_.ProcessName -match "note"} &#124; Stop-Process -Whatif<br />
What if: Performing operation "Stop-Process" on Target "notepad (4284)".<br />
What if: Performing operation "Stop-Process" on Target "notepad (4524)".<br />
What if: Performing operation "Stop-Process" on Target "notepad (4848)".<br />
What if: Performing operation "Stop-Process" on Target "notepad++ (4684)".<br />
What if: Performing operation "Stop-Process" on Target "ONENOTE (5944)".<br />
What if: Performing operation "Stop-Process" on Target "ONENOTEM (3876)".<br />
PS C:\&#62;</code></p>

<p>This would have wiped out every instance of notepad.exe along with my Onenote and notepad++ processes which was not what I wanted.</p>
<h3>Lame example?</h3>
<p>Yes, I guess you&#8217;re right that was quite a lame example. However I think it does illustrate how you can use the -whatif parameter. It can be very useful if you are working with important data and don&#8217;t want to make mistakes. Also it&#8217;s useful if your just playing around with PowerShell and learning what all the fuss is about.</p>
]]></description>
			<content:encoded><![CDATA[<p><img class="left" src="http://scriptlore.com/images/sx/2008/powershell-parameter-whatif.jpg" alt="PowerShell WhatIf Parameter" /></p>
<p>What if you just deleted 5000 accounts from your active directory domain by accident, what if there had been an easy way to make sure you were doing the right thing before the deletion. Would you have wanted to use it? I know I would. Just imagine you were planning on deleting all accounts which had been disabled for six months but you forgot some important switch and most of your accounts were gone because of that stupid switch.<br />
<span id="more-21"></span><br />
Ok, so this might be a bit far fetched but these things can happen and if you are not 100 percent sure of what you are doing, it&#8217;s great to be able to see what the results of your actions will be before you take them.</p>
<p>Let&#8217;s say we&#8217;ve been taking random notes and just want to kill all notepad processes without having to choose if we want to save every single file. Working with the Get-Process, Where-Object and Stop-Process cmdlets we decide to kill all the processes which has &#8220;note&#8221; in them.</p>
<p><code>PS C:\&gt; Get-Process | Where-Object {$_.ProcessName -match "note"} | Stop-Process -Whatif<br />
What if: Performing operation "Stop-Process" on Target "notepad (4284)".<br />
What if: Performing operation "Stop-Process" on Target "notepad (4524)".<br />
What if: Performing operation "Stop-Process" on Target "notepad (4848)".<br />
What if: Performing operation "Stop-Process" on Target "notepad++ (4684)".<br />
What if: Performing operation "Stop-Process" on Target "ONENOTE (5944)".<br />
What if: Performing operation "Stop-Process" on Target "ONENOTEM (3876)".<br />
PS C:\&gt;</code></p>
<p>This would have wiped out every instance of notepad.exe along with my Onenote and notepad++ processes which was not what I wanted.</p>
<h3>Lame example?</h3>
<p>Yes, I guess you&#8217;re right that was quite a lame example. However I think it does illustrate how you can use the -whatif parameter. It can be very useful if you are working with important data and don&#8217;t want to make mistakes. Also it&#8217;s useful if your just playing around with PowerShell and learning what all the fuss is about.</p>
]]></content:encoded>
			<wfw:commentRss>http://scriptlore.com/powershell/whatif/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ErrorVariable, Parameter</title>
		<link>http://scriptlore.com/powershell/errorvariable/</link>
		<comments>http://scriptlore.com/powershell/errorvariable/#comments</comments>
		<pubDate>Mon, 26 May 2008 15:15:51 +0000</pubDate>
		<dc:creator>pogenstad</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Basics]]></category>
		<category><![CDATA[Parameters]]></category>

		<guid isPermaLink="false">http://scriptlore.com/?p=18</guid>
		<description><![CDATA[<p><img class="left" src="http://scriptlore.com/images/sx/2008/powershell-parameter-errorvariable.jpg" alt="PowerShell ErrorVariable Parameter" /></p>
<p>It is said that we learn from our mistakes, and that to improve learning it&#8217;s best to write things down. How about storing our mistakes and errors in a variable? If that&#8217;s not killing two flies with one stone, at least it&#8217;s a useful feature of PowerShell. Let&#8217;s use it and see how it works, using the Stop-Service cmdlet we try to stop a service:</p>

<p><code>PS C:\&#62; Stop-Service -name XAudioService -ErrorVariable myErrors<br />
Stop-Service : Service 'XAudioService (XAudioService)' cannot be stopped due to the following error: Cannot open XAudio<br />
Service service on computer '.'.<br />
At line:1 char:13<br />
+ Stop-Service  &#60;&#60;&#60;</code></p>
<p>As you can see the error is also written to screen. From now on we can reference the error in the $myErrors variable we created:</p>
<p><code>PS C:\&#62; $myErrors<br />

Stop-Service : Service 'XAudioService (XAudioService)' cannot be stopped due to the following error: Cannot open XAudio<br />
Service service on computer '.'.<br />
At line:1 char:13<br />
+ Stop-Service  &#60;&#60;&#60;</code></p>
<p>You might want to know that even if you don&#8217;t specify the -ErrorVariable parameter all the errors will be storied in a special error called $error.</p>
<p>Saving the error variable like we have done above will ensure that you have the last error saved. One thing you can do is to add a plus sign before the variable when you want to append to the error variable. Here is an example:</p>
<p><code>PS C:\&#62; Stop-Service -name XAudioService -ErrorVariable +myError<br />

PS C:\&#62; Stop-Service -name WinRM -ErrorVariable +myError<br />
PS C:\&#62; $myError.Count<br />
2<br />
PS C:\&#62;</code></p>]]></description>
			<content:encoded><![CDATA[<p><img class="left" src="http://scriptlore.com/images/sx/2008/powershell-parameter-errorvariable.jpg" alt="PowerShell ErrorVariable Parameter" /></p>
<p>It is said that we learn from our mistakes, and that to improve learning it&#8217;s best to write things down. How about storing our mistakes and errors in a variable? If that&#8217;s not killing two flies with one stone, at least it&#8217;s a useful feature of PowerShell. Let&#8217;s use it and see how it works, using the Stop-Service cmdlet we try to stop a service: <span id="more-18"></span></p>
<p><code>PS C:\&gt; Stop-Service -name XAudioService -ErrorVariable myErrors<br />
Stop-Service : Service 'XAudioService (XAudioService)' cannot be stopped due to the following error: Cannot open XAudio<br />
Service service on computer '.'.<br />
At line:1 char:13<br />
+ Stop-Service  &lt;&lt;&lt;</code></p>
<p>As you can see the error is also written to screen. From now on we can reference the error in the $myErrors variable we created:</p>
<p><code>PS C:\&gt; $myErrors<br />
Stop-Service : Service 'XAudioService (XAudioService)' cannot be stopped due to the following error: Cannot open XAudio<br />
Service service on computer '.'.<br />
At line:1 char:13<br />
+ Stop-Service  &lt;&lt;&lt;</code></p>
<p>You might want to know that even if you don&#8217;t specify the -ErrorVariable parameter all the errors will be storied in a special error called $error.</p>
<p>Saving the error variable like we have done above will ensure that you have the last error saved. One thing you can do is to add a plus sign before the variable when you want to append to the error variable. Here is an example:</p>
<p><code>PS C:\&gt; Stop-Service -name XAudioService -ErrorVariable +myError<br />
PS C:\&gt; Stop-Service -name WinRM -ErrorVariable +myError<br />
PS C:\&gt; $myError.Count<br />
2<br />
PS C:\&gt;</code></p>
]]></content:encoded>
			<wfw:commentRss>http://scriptlore.com/powershell/errorvariable/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Confirm, Parameter</title>
		<link>http://scriptlore.com/powershell/confirm/</link>
		<comments>http://scriptlore.com/powershell/confirm/#comments</comments>
		<pubDate>Wed, 21 May 2008 14:34:35 +0000</pubDate>
		<dc:creator>pogenstad</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Basics]]></category>
		<category><![CDATA[Parameters]]></category>

		<guid isPermaLink="false">http://scriptlore.com/?p=15</guid>
		<description><![CDATA[<img class="left" src="http://scriptlore.com/images/sx/2008/powershell-parameter-confirm.jpg" alt="PowerShell Confirm Parameter" />

Imagine you are feeling handy and start out to rebuild your bathroom. Now imagine you're standing knee deep in water in your basement. I bet you wished that somewhere in your head there was a tiny voice which had asked you, "do you <em>really</em> want to drill there"? This is the purpose of the PowerShell -Confirm parameter. If you're running a cmdlet which changes the system state you can use -Confirm in order to get a question asking you if you really want to go ahead with the change.

<h3>Argh, my files, my precious files!</h3>

We've all heard horror stories of format c: and the like. At one point you might even have deleted files by mistake. With the Remove-Item cmdlet you can do just that.

<code>PS C:\Temp&#62; Remove-Item *
PS C:\Temp&#62;</code> &#60;- That went smooth!

However wouldn't this have been better:

<code>PS C:\Temp&#62; Remove-Item * -Confirm</code>

<code>Confirm
Are you sure you want to perform this action?
Performing operation "Remove File" on Target "C:\Temp\junkfile1.txt".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is &#34;Y&#34;): y</code>

<code>Confirm
Are you sure you want to perform this action?
Performing operation "Remove File" on Target "C:\Temp\junkfile2.txt".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is &#34;Y&#34;): y</code>

<code>Confirm
Are you sure you want to perform this action?
Performing operation "Remove File" on Target "C:\Temp\junkfile3.txt".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is &#34;Y&#34;): y</code>

<code>Confirm
Are you sure you want to perform this action?
Performing operation "Remove File" on Target "C:\Temp\single-copy-of-expense-report.xlsx".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is &#34;Y&#34;): n</code>

<h3>Lessons Learned</h3>

As the carpenters say; measure twice, cut once. Sometimes it's better to step carefully unless you are really sure of what you are doing. The -Confirm parameter can be that little voice inside your head.]]></description>
			<content:encoded><![CDATA[<p><img class="left" src="http://scriptlore.com/images/sx/2008/powershell-parameter-confirm.jpg" alt="PowerShell Confirm Parameter" /></p>
<p>Imagine you are feeling handy and start out to rebuild your bathroom. Now imagine you&#8217;re standing knee deep in water in your basement. I bet you wished that somewhere in your head there was a tiny voice which had asked you, &#8220;do you <em>really</em> want to drill there&#8221;? This is the purpose of the PowerShell -Confirm parameter. If you&#8217;re running a cmdlet which changes the system state you can use -Confirm in order to get a question asking you if you really want to go ahead with the change.<br />
<span id="more-15"></span></p>
<h3>Argh, my files, my precious files!</h3>
<p>We&#8217;ve all heard horror stories of format c: and the like. At one point you might even have deleted files by mistake. With the Remove-Item cmdlet you can do just that.</p>
<p><code>PS C:\Temp&gt; Remove-Item *<br />
PS C:\Temp&gt;</code> &lt;- That went smooth!</p>
<p>However wouldn&#8217;t this have been better:</p>
<p><code>PS C:\Temp&gt; Remove-Item * -Confirm</code></p>
<p><code>Confirm<br />
Are you sure you want to perform this action?<br />
Performing operation "Remove File" on Target "C:\Temp\junkfile1.txt".<br />
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is &quot;Y&quot;): y</code></p>
<p><code>Confirm<br />
Are you sure you want to perform this action?<br />
Performing operation "Remove File" on Target "C:\Temp\junkfile2.txt".<br />
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is &quot;Y&quot;): y</code></p>
<p><code>Confirm<br />
Are you sure you want to perform this action?<br />
Performing operation "Remove File" on Target "C:\Temp\junkfile3.txt".<br />
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is &quot;Y&quot;): y</code></p>
<p><code>Confirm<br />
Are you sure you want to perform this action?<br />
Performing operation "Remove File" on Target "C:\Temp\single-copy-of-expense-report.xlsx".<br />
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is &quot;Y&quot;): n</code></p>
<h3>Lessons Learned</h3>
<p>As the carpenters say; measure twice, cut once. Sometimes it&#8217;s better to step carefully unless you are really sure of what you are doing. The -Confirm parameter can be that little voice inside your head.</p>
]]></content:encoded>
			<wfw:commentRss>http://scriptlore.com/powershell/confirm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OutVariable, Parameter</title>
		<link>http://scriptlore.com/powershell/outvariable/</link>
		<comments>http://scriptlore.com/powershell/outvariable/#comments</comments>
		<pubDate>Mon, 19 May 2008 21:30:18 +0000</pubDate>
		<dc:creator>pogenstad</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Basics]]></category>
		<category><![CDATA[Parameters]]></category>

		<guid isPermaLink="false">http://scriptlore.com/?p=19</guid>
		<description><![CDATA[Building on the Perl motto; &#8220;There&#8217;s more than one way to do it&#8221;, PowerShell lovers will tell you that they can play too. Basically the -OutVariable parameter stores the output from a cmdlet to a variable and at the same time letting the cmdlet display all the output to the screen. For example looking at [...]]]></description>
			<content:encoded><![CDATA[<p><img class="left" src="http://scriptlore.com/images/sx/2008/powershell-parameter-outvariable.jpg" alt="PowerShell OutVariable Parameter" /></p>
<p>Building on the Perl motto; &#8220;There&#8217;s more than one way to do it&#8221;, PowerShell lovers will tell you that they can play too. Basically the -OutVariable parameter stores the output from a cmdlet to a variable and at the same time letting the cmdlet display all the output to the screen. For example looking at the Get-ChildItem cmdlet you can store the output in a variable by doing:</p>
<p><span id="more-19"></span><code>$myChildren = Get-ChildItem</code></p>
<p>Using the -OutVariable parameter you would instead type:</p>
<p><code>Get-ChildItem -OutVariable myChildren</code></p>
<p>This would also store the output in the myChildren variable (notice the lack of the $ sign), but unlike the first example the output would be printed to the screen. </p>
<p>This might sound a lot like the Tee-Object cmdlet. However you can also use OutVariable like this:</p>
<p><code>Get-ChildItem Directory1 -OutVariable myChildren<br />
Get-ChildItem Directory2 -OutVariable +myChildren</code></p>
<p>This way the $myChildren variable would hold the contents of both Directory1 and Directory2. Another difference, if you&#8217;re in a hurry and want to juice out some performance, can be shown by the Measure-Command cmdlet.</p>
<p><code>Measure-Command {Get-ChildItem | Tee-Object -variable myTeeOutput}<br />
TotalMilliseconds : 3,3151</code></p>
<p><code>Measure-Command {Get-ChildItem -OutVariable myOutVariableOutput}<br />
TotalMilliseconds : 2,0668</code></p>
<p>Folks, we have a winner! But what will we do with all the extra free time?</p>
<p>If you just want to store the variable and not display the output you can use the Out-Null cmdlet:</p>
<p><code>Get-ChildItem -OutVariable myChildren | Out-Null</code></p>
]]></content:encoded>
			<wfw:commentRss>http://scriptlore.com/powershell/outvariable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get-ExecutionPolicy</title>
		<link>http://scriptlore.com/powershell/get-executionpolicy/</link>
		<comments>http://scriptlore.com/powershell/get-executionpolicy/#comments</comments>
		<pubDate>Wed, 07 May 2008 21:51:01 +0000</pubDate>
		<dc:creator>pogenstad</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Basics]]></category>
		<category><![CDATA[Cmdlet]]></category>
		<category><![CDATA[Native]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://scriptlore.com/?p=5</guid>
		<description><![CDATA[PowerShell uses for different settings called Execution Policy which governs how scripts will run on your computer and if they need to be digitally signed. Get-ExecutionPolicy is a Cmdlet which allows you to view which Execution Policy that is currently used by your computer. SYNTAX Get-ExecutionPolicy -Verbose -Debug -ErrorAction -ErrorVariable -OutVariable Examples PS C:\&#62; Get-ExecutionPolicy [...]]]></description>
			<content:encoded><![CDATA[<p><img class="left" src="http://scriptlore.com/images/sx/2008/cmdlet-get-executionpolicy.jpg" alt="Get-ExecutionPolicy" /></p>
<p>PowerShell uses for different settings called <a href="http://scriptlore.com/powershell/execution-policy/" title="PowerShell Execution Policy">Execution Policy</a> which governs how scripts will run on your computer and if they need to be digitally signed. Get-ExecutionPolicy is a Cmdlet which allows you to view which Execution Policy that is currently used by your computer. <span id="more-5"></span> </p>
<h3>SYNTAX</h3>
<p>Get-ExecutionPolicy<br />
<a href="http://scriptlore.com/powershell/verbose/">-Verbose</a><br />
<a href="http://scriptlore.com/powershell/debug/">-Debug</a><br />
<a href="http://scriptlore.com/powershell/erroraction/" title="PowerShell ErrorAction Parameter">-ErrorAction</a><br />
<a href="http://scriptlore.com/powershell/errorvariable/" title="PowerShell ErrorVariable Parameter">-ErrorVariable</a><br />
<a href="http://scriptlore.com/powershell/outvariable/" title="PowerShell OutVariable Parameter">-OutVariable</a></p>
<h3>Examples</h3>
<p>PS C:\&gt; Get-ExecutionPolicy<br />
Restricted</p>
]]></content:encoded>
			<wfw:commentRss>http://scriptlore.com/powershell/get-executionpolicy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Concerning Execution Policy</title>
		<link>http://scriptlore.com/powershell/execution-policy/</link>
		<comments>http://scriptlore.com/powershell/execution-policy/#comments</comments>
		<pubDate>Thu, 01 May 2008 09:23:23 +0000</pubDate>
		<dc:creator>pogenstad</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Basics]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://scriptlore.com/?p=1</guid>
		<description><![CDATA[Probably the first thing you ever do whenever you learn a new language is to write your hello world program, however in PowerShell the most simple script will cause it to crash! Instead of the friendly greeting PowerShell will spit back: &#8220;hello.ps1 cannot be loaded because the execution of scripts is disabled on this system.&#8221; [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://scriptlore.com/images/sx/2008/execution-policy.jpg" alt="Execution Policy" class="left" /></p>
<p>Probably the first thing you ever do whenever you learn a new language is to write your <a href="http://scriptlore.com/about/hello-world/" title="Your first script">hello world program</a>, however in PowerShell the most simple script will cause it to crash!</p>
<p>Instead of the friendly greeting PowerShell will spit back: <span id="more-1"></span> &#8220;hello.ps1 cannot be loaded because the execution of scripts is disabled on this system.&#8221;</p>
<p>The error is fairly straight forward though, the Execution Policy governs whether you are able to run scripts or not. If you want to run scripts at all you have to change the execution policy.</p>
<h3>Unrestricted</h3>
<p><img src="http://scriptlore.com/images/sx/2008/execution-policy-unrestricted.jpg" alt="Unrestricted" class="left" /></p>
<p>Unrestricted is the careless cowboy setting. If you set the Execution Policy to Unrestricted PowerShell will run any script regardless if it has a digital signature or not. As you might have guessed this is the worst setting in terms of security. Some things are fun to do in the short term, but you never know what kind of troubles you face down the road.</p>
<h3>RemoteSigned</h3>
<p><img src="http://scriptlore.com/images/sx/2008/execution-policy-remotesigned.jpg" alt="RemoteSigned" class="left" /></p>
<p>Using the RemoteSigned Execution Policy means that PowerShell will require that scripts downloaded from the Internet are signed by a trusted source. However you won&#8217;t have to signed scripts stored locally on your machine. This is a great setting if you just want to learn some scripting and take PowerShell for a test drive and don&#8217;t want to dive into the script signing business. Besides, you can always get your PKI environment up tomorrow.</p>
<h3>AllSigned</h3>
<p><img src="http://scriptlore.com/images/sx/2008/execution-policy-allsigned.jpg" alt="AllSigned" class="left" /></p>
<p>The AllSigned policy requires every script you want to run to be signed by a trusted certificate, if you want to run scripts this is the setting which will let your sysadmin sleep soundly at night. In order to run your scripts you will have to get some <a href="http://scriptlore.com/script-signing-how-to/" title="Script signing guide">script signing</a> procedures in place.</p>
<h3>Restricted</h3>
<p><img src="http://scriptlore.com/images/sx/2008/execution-policy-restricted.jpg" alt="Restricted" class="left" /></p>
<p>The Restricted option means that PowerShell will not run any script at all not even if it is digitally signed. You might be thinking that this would make PowerShell quite worthless as a scripting language, and you might be right. However PowerShell is also a shell and you can use it without ever writing a single script. Restricted is the default Execution Policy which is why your scripts won’t run when you start using PowerShell. In order to be able to run scripts you have to change the Execution Policy.</p>
<h3>Changing the Execution Policy</h3>
<p>You can change the Execution Policy with the Set-ExecutionPolicy cmdlet, to view your current Execution Policy you can use the <a href="http://scriptlore.com/powershell/get-executionpolicy/" title="Get-ExecutionPolicy Cmdlet">Get-ExecutionPolicy</a> cmdlet.</p>
]]></content:encoded>
			<wfw:commentRss>http://scriptlore.com/powershell/execution-policy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

