<?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>API Programming &#187; PowerShell</title>
	<atom:link href="http://www.brangle.com/wordpress/category/tutorials/powershell/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.brangle.com/wordpress</link>
	<description>Just another computer weblog</description>
	<lastBuildDate>Wed, 14 Dec 2011 07:35:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Download File with a PowerShell One-Liner</title>
		<link>http://www.brangle.com/wordpress/2011/12/download-file-with-a-powershell-one-liner/</link>
		<comments>http://www.brangle.com/wordpress/2011/12/download-file-with-a-powershell-one-liner/#comments</comments>
		<pubDate>Mon, 12 Dec 2011 04:33:58 +0000</pubDate>
		<dc:creator>lopezg</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Windows PowerShell]]></category>

		<guid isPermaLink="false">http://www.brangle.com/wordpress/?p=679</guid>
		<description><![CDATA[I was recently trying to automate some scripts on a linux box and I wanted to know if it was as simple to download a file in PowerShell as it is in linux: <code>wget http://example.com/file.jpg</code> <br />Well as it turned out, it wasn't, but it wasn't that difficult either. What we needed was too use the <a href="http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx" target="_blank">System.Net.WebClient</a> class. There's one thing to note, in PowerShell you can't use a ~ (tilde) to reference your home directly, that is to say, you can't download a file to <code>~\Desktop</code> you will need to specify the full path, in my case <code>C:\Users\Brangle\Desktop</code>. [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently trying to automate some scripts on a linux box and I wanted to know if it was as simple to download a file in PowerShell as it is in linux: <code>wget http://example.com/file.jpg</code><br />
<br />Well as it turned out, it wasn&#8217;t, but it wasn&#8217;t that difficult either.  What we needed was too use the <a href="http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx" target="_blank">System.Net.WebClient</a> class.  There&#8217;s one thing to note, in PowerShell you can&#8217;t use a ~ (tilde) to reference your home directly, that is to say, you can&#8217;t download a file to <code>~\Desktop</code> you will need to specify the full path, in my case <code>C:\Users\Brangle\Desktop</code>.<span id="more-679"></span></p>
<p><br /></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">new-object</span> System.Net.WebClient<span style="color: #000000;">&#41;</span>.Downloadfile<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;http://wordpress.org/latest.zip&quot;</span><span style="color: pink;">,</span> <span style="color: #800000;">&quot;C:\Users\Brangle\Desktop\wp-latest.zip&quot;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.brangle.com/wordpress/2011/12/download-file-with-a-powershell-one-liner/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Make PowerShell Talk / Speak</title>
		<link>http://www.brangle.com/wordpress/2011/12/make-powershell-talk-speak/</link>
		<comments>http://www.brangle.com/wordpress/2011/12/make-powershell-talk-speak/#comments</comments>
		<pubDate>Sat, 10 Dec 2011 05:41:15 +0000</pubDate>
		<dc:creator>lopezg</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Windows PowerShell]]></category>

		<guid isPermaLink="false">http://www.brangle.com/wordpress/?p=644</guid>
		<description><![CDATA[Have you ever run a long PowerShell script, minimized it, only to realize much later that an error caused it to stop? Rather than just using a color syntax red to display the error,&#160; I wanted to hear it so I could switch back to the PowerShell window and see the error. So how easy is it to make PowerShell speak? It’s almost identical to the .NET code I have in VB.net and C# on <a target="_blank" href="http://www.brangle.com/wordpress/2009/08/make-your-computer-talk-with-vb-net-application-and-source-code/">Make Your Computer Talk with VB.NET Application (and Source Code)</a> using the SAPI.SpVoice class. [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever run a long PowerShell script, minimized it, only to realize much later that an error caused it to stop? Rather than just using a color syntax red to display the error,&nbsp; I wanted to hear it so I could switch back to the PowerShell window and see the error.</p>
<p>So how easy is it to make PowerShell speak? It’s almost identical to the .NET code I have in VB.net and C# on <a target="_blank" href="http://www.brangle.com/wordpress/2009/08/make-your-computer-talk-with-vb-net-application-and-source-code/">Make Your Computer Talk with VB.NET Application (and Source Code)</a> using the SAPI.SpVoice class.<span id="more-644"></span></p>
<p>Here’s the one-liner:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">new-object</span> <span style="color: pink;">-</span>com SAPI.SpVoice<span style="color: #000000;">&#41;</span>.speak<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Welcome to brangle dot com&quot;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p>Now, if we want to write a function to generate to use whenever needed we could do so just like below. You might notice we used a global:functionName, this will allow the function to be in the global scope. If you don&#8217;t know what the global scope is, then just use, you&#8217;ll probably want it rather than not.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">function</span> global:talk <span style="color: #000000;">&#40;</span><span style="color: #800080;">$message</span><span style="color: #000000;">&#41;</span>  <span style="color: #000000;">&#123;</span>
   <span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">new-object</span> <span style="color: pink;">-</span>com SAPI.SpVoice<span style="color: #000000;">&#41;</span>.speak<span style="color: #000000;">&#40;</span><span style="color: #800080;">$message</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #800080;">$error</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;There was an error pinging the webserver&quot;</span>
talk<span style="color: #000000;">&#40;</span><span style="color: #800080;">$error</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.brangle.com/wordpress/2011/12/make-powershell-talk-speak/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Combine or Join Two Text Files Using PowerShell</title>
		<link>http://www.brangle.com/wordpress/2009/08/combine-join-two-text-files-using-powershell/</link>
		<comments>http://www.brangle.com/wordpress/2009/08/combine-join-two-text-files-using-powershell/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 03:28:37 +0000</pubDate>
		<dc:creator>lopezg</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Windows PowerShell]]></category>

		<guid isPermaLink="false">http://www.brangle.com/wordpress/?p=422</guid>
		<description><![CDATA[Merging files together, or to a seperate file, is a snap with PowerShell using the <i>Get-Content</i> cmdlet. If you've never used the Add-Content cmdlet, I would suggest first reading my other article <a href="/wordpress/2009/08/append-text-to-a-file-using-add-content-in-powershell/" target="_top">Append Text to a File Using Add-Content</a>. The first thing we need to do is identify our two files, in my case I'll be using <i>file1.log</i> and <i>file2.log</i>. In the first example, I append all the text of <i>file2.log</i> into the end of <i>file1.log</i>. In the second example, I'll create a completely new file and dump the contents of both the files into the newly created file. In the last example, I'll add the current date to the filename of a completly new log file. [...]]]></description>
			<content:encoded><![CDATA[<p>Merging files together, or to a seperate file, is a snap with PowerShell using the <i>Get-Content</i> cmdlet.  If you&#8217;ve never used the Add-Content cmdlet, I would suggest first reading my other article <a href="http://www.brangle.com/wordpress/2009/08/append-text-to-a-file-using-add-content-in-powershell/" target="_top">Append Text to a File Using Add-Content</a>.  The first thing we need to do is identify our two files, in my case I&#8217;ll be using <i>file1.log</i> and <i>file2.log</i>.  In the first example, I append all the text of <i>file2.log</i> into the end of <i>file1.log</i>.  In the second example, I&#8217;ll create a completely new file and dump the contents of both the files into the newly created file.  In the last example, I&#8217;ll add the current date to the filename of a completly new log file.<span id="more-422"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$file2</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> <span style="color: #800000;">&quot;file2.log&quot;</span>
<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #800000;">&quot;file1.log&quot;</span> $file2</pre></td></tr></table></div>

<p>Now, what if we want to copy the contents of <i>file1.log</i> and <i>file2.log</i> into a completely new file called <i>file3.log</i></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">New-Item</span> <span style="color: #008080; font-style: italic;">-ItemType</span> file <span style="color: #800000;">&quot;file3.log&quot;</span>
<span style="color: #800080;">$file1</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> <span style="color: #800000;">&quot;file1.log&quot;</span>
<span style="color: #800080;">$file2</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> <span style="color: #800000;">&quot;file2.log&quot;</span>
<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #800000;">&quot;file3.log&quot;</span> <span style="color: #800080;">$file1</span>
<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #800000;">&quot;file3.log&quot;</span> $file2</pre></td></tr></table></div>

<p>What if we want to erase everything in file3.log before we add new content? In this case, instead of creating a new file, we&#8217;ll use the Clear-Contents cmdlet.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Clear-Content</span> <span style="color: #800000;">&quot;file3.log&quot;</span>
<span style="color: #800080;">$file1</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> <span style="color: #800000;">&quot;file1.log&quot;</span>
<span style="color: #800080;">$file2</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> <span style="color: #800000;">&quot;file2.log&quot;</span>
<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #800000;">&quot;file3.log&quot;</span> <span style="color: #800080;">$file1</span>
<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #800000;">&quot;file3.log&quot;</span> $file2</pre></td></tr></table></div>

<p>Ok, well, what if I don&#8217;t know if file3.log already exists? If it doesn&#8217;t exist and I run the Clear-Contents cmdlet then I&#8217;ll get an error. And if it does exist and I try to create it as a new file I&#8217;ll still get an error.  There&#8217;s an easy way to do this, we&#8217;ll use the New-Item cmdlet from the example above, but we&#8217;ll add the -force argument, so even if the file exists it will overwrite it.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">New-Item</span> <span style="color: #008080; font-style: italic;">-ItemType</span> file <span style="color: #800000;">&quot;file3.log&quot;</span> <span style="color: #008080; font-style: italic;">-force</span>
<span style="color: #800080;">$file1</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> <span style="color: #800000;">&quot;file1.log&quot;</span>
<span style="color: #800080;">$file2</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> <span style="color: #800000;">&quot;file2.log&quot;</span>
<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #800000;">&quot;file3.log&quot;</span> <span style="color: #800080;">$file1</span>
<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #800000;">&quot;file3.log&quot;</span> $file2</pre></td></tr></table></div>

<p>Alright, so I guess I got the hang of this, now lastly since these are log files, how can I append the date to the file name? Simple, with the <i>Get-Date</i> cmdlet.  Let&#8217;s output our file name as YYYYMMDD-WebAccess.log in this example.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$date</span><span style="color: pink;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Get-Date</span><span style="color: #000000;">&#41;</span>.ToString<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;yyyyMMdd&quot;</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$NewFileName</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;$date-WebAccess.log&quot;</span>
<span style="color: #008080; font-weight: bold;">New-Item</span> <span style="color: #008080; font-style: italic;">-ItemType</span> file <span style="color: #800080;">$NewFileName</span>
<span style="color: #800080;">$file1</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> <span style="color: #800000;">&quot;file1.log&quot;</span>
<span style="color: #800080;">$file2</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> <span style="color: #800000;">&quot;file2.log&quot;</span>
<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #800080;">$NewFileName</span> <span style="color: #800080;">$file1</span>
<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #800080;">$NewFileName</span> $file2</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.brangle.com/wordpress/2009/08/combine-join-two-text-files-using-powershell/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Retrieve File Permissions using Get-Acl with PowerShell</title>
		<link>http://www.brangle.com/wordpress/2009/08/retrieve-file-permissions-using-get-acl-with-powershell/</link>
		<comments>http://www.brangle.com/wordpress/2009/08/retrieve-file-permissions-using-get-acl-with-powershell/#comments</comments>
		<pubDate>Tue, 25 Aug 2009 06:30:24 +0000</pubDate>
		<dc:creator>lopezg</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Windows PowerShell]]></category>

		<guid isPermaLink="false">http://www.brangle.com/wordpress/?p=385</guid>
		<description><![CDATA[Retrieving the owner and permissions of a file, folders and even registry keys is a breeze with PowerShell's Get-Acl cmdlet. Let's try checking to see who has access to to the <i>C:\Documents and Settings\Administrator</i> folder. [...]]]></description>
			<content:encoded><![CDATA[<p>Retrieving the owner and permissions of a file, folders and even registry keys is a breeze with PowerShell&#8217;s Get-Acl cmdlet.  Let&#8217;s try checking to see who has access to to the <i>C:\Documents and Settings\Administrator</i> folder.<span id="more-385"></span></p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Get-Acl</span> <span style="color: #800000;">&quot;C:\Documents and Settings\Administrator&quot;</span></pre></div></div>

<p> Now this return everything that I would expect it to, but I prefer the <i>Format-List</i> view, so let&#8217;s check the permissions on the the file <i>C:\Windows\clock.avi</i> and output it in a list view.</p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Get-Acl</span> <span style="color: #800000;">&quot;C:\Windows\clock.avi&quot;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Format-List</span></pre></div></div>

<p>Ok, now I did say we could check the permissions of a registry key, so let&#8217;s go for it&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Get-Acl</span> HKLM:\Software\Microsoft</pre></td></tr></table></div>

<p>Now if you just want to find out the owner of the file you could pull back just that value</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Get-Acl</span> <span style="color: #800000;">&quot;C:\Windows\clock.avi&quot;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Select</span> Owner</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.brangle.com/wordpress/2009/08/retrieve-file-permissions-using-get-acl-with-powershell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Ping a Computer using PowerShell</title>
		<link>http://www.brangle.com/wordpress/2009/08/how-to-ping-a-computer-using-powershell/</link>
		<comments>http://www.brangle.com/wordpress/2009/08/how-to-ping-a-computer-using-powershell/#comments</comments>
		<pubDate>Tue, 25 Aug 2009 04:22:19 +0000</pubDate>
		<dc:creator>lopezg</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Windows PowerShell]]></category>

		<guid isPermaLink="false">http://www.brangle.com/wordpress/?p=380</guid>
		<description><![CDATA[Pinging a computer is really simple, and of course since this is PowerShell we don't want to just use something as primitive as <i>ping.exe</i>, we want to use .NET and take advantage of all the method calls and filters. [...]]]></description>
			<content:encoded><![CDATA[<p>Pinging a computer is really simple, and of course since this is PowerShell we don&#8217;t want to use something as primitive as <i>ping.exe</i>, we want to use .NET and take advantage of all the method calls and filters.<span id="more-380"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$ping</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Net.NetworkInformation.Ping
<span style="color: #800080;">$ping</span>.Send<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;www.brangle.com&quot;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p>We can also set a timeout so that our computer doesn&#8217;t hang to long on a server that won&#8217;t respond. Let&#8217;s add a timeout of a 5 seconds, we will first need to convert this to milliseconds, so that&#8217;s 5,000ms. Of course 5 seconds may not be a resonable timeout, you need to figure out what&#8217;s appropriate for you and what you&#8217;re willing to tolerate.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$ping</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> System.Net.NetworkInformation.Ping
<span style="color: #800080;">$ping</span>.Send<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;www.brangle.com&quot;</span><span style="color: pink;">,</span> <span style="color: #804000;">5000</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p>Technical details: System.Net.NetworkInformation.Ping requires that you have .NET 2.0 or greater.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.brangle.com/wordpress/2009/08/how-to-ping-a-computer-using-powershell/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Using the System.Environment Class in PowerShell</title>
		<link>http://www.brangle.com/wordpress/2009/08/using-the-system-environment-class-in-powershell/</link>
		<comments>http://www.brangle.com/wordpress/2009/08/using-the-system-environment-class-in-powershell/#comments</comments>
		<pubDate>Mon, 24 Aug 2009 08:24:58 +0000</pubDate>
		<dc:creator>lopezg</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Windows PowerShell]]></category>

		<guid isPermaLink="false">http://www.brangle.com/wordpress/?p=369</guid>
		<description><![CDATA[The Environment class has a huge array of information and accessing this information is very simple, I'm going to try and give a whole bunch of one line examples of how to do this using PowerShell. [...]]]></description>
			<content:encoded><![CDATA[<p>The Environment class has a huge array of information and accessing this information is very simple, I&#8217;m going to try and give a whole bunch of one line examples of how to do this using PowerShell.<span id="more-369"></span></p>
<p>To retrieve the comptuer name</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>System.Environment<span style="color: #000000;">&#93;</span>::MachineName</pre></td></tr></table></div>

<p>Get Operating System Information (such as Platform, service pack, version, and version string)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>System.Environment<span style="color: #000000;">&#93;</span>::OSVersion</pre></td></tr></table></div>

<p>Checking to see if your computer has started to shutdown</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>System.Environment<span style="color: #000000;">&#93;</span>::HasShutdownStarted</pre></td></tr></table></div>

<p>Instead of trying to remember if you should use <i>`r`n</i> or is it just <i>`n</i>? A simple way is just to get .NET to tell you and assign this value to a variable.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$nl</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>System.Environment<span style="color: #000000;">&#93;</span>::NewLine</pre></td></tr></table></div>

<p>Get the system directory (normally this is C:\Windows\System32), but for some computers it could be different</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>System.Environment<span style="color: #000000;">&#93;</span>::SystemDirectory</pre></td></tr></table></div>

<p>Get the user&#8217;s domain name</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>System.Environment<span style="color: #000000;">&#93;</span>::UserDomainName</pre></td></tr></table></div>

<p>Get the user&#8217;s name</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>System.Environment<span style="color: #000000;">&#93;</span>::UserName</pre></td></tr></table></div>

<p>Get a list of all environment variables</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>System.Environment<span style="color: #000000;">&#93;</span>::GetEnvironmentVariables<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p>Get a particular environment variable</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$path</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>System.Environment<span style="color: #000000;">&#93;</span>::GetEnvironmentVariable<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;path&quot;</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$temp</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>System.Environment<span style="color: #000000;">&#93;</span>::GetEnvironmentVariable<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;temp&quot;</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$os</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>System.Environment<span style="color: #000000;">&#93;</span>::GetEnvironmentVariable<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;os&quot;</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$homedrive</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>System.Environment<span style="color: #000000;">&#93;</span>::GetEnvironmentVariable<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;homedrive&quot;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p>Create a new environment variable, or overwrite an existing one</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>System.Environment<span style="color: #000000;">&#93;</span>::SetEnvironmentVariable<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;VariableName&quot;</span><span style="color: pink;">,</span><span style="color: #800000;">&quot;VariableValue&quot;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p>Get all logical drives</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>System.Environment<span style="color: #000000;">&#93;</span>::GetLogicalDrives<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.brangle.com/wordpress/2009/08/using-the-system-environment-class-in-powershell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Send eMail and SMS Text Messages with PowerShell</title>
		<link>http://www.brangle.com/wordpress/2009/08/send-email-messages-with-powershell/</link>
		<comments>http://www.brangle.com/wordpress/2009/08/send-email-messages-with-powershell/#comments</comments>
		<pubDate>Mon, 24 Aug 2009 05:03:04 +0000</pubDate>
		<dc:creator>lopezg</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Windows Management Instrumentation (WMI)]]></category>
		<category><![CDATA[Windows PowerShell]]></category>

		<guid isPermaLink="false">http://www.brangle.com/wordpress/?p=358</guid>
		<description><![CDATA[Thanks to the .NET library sending emails with PowerShell is surprisingly very simple! One thing to remember is line breaks, when you need a new line, make sure to use the escape characters <i>`n</i> . In the first example, I'll send just a regular email, in the second I'll send a SMS text message. [...]]]></description>
			<content:encoded><![CDATA[<p>Thanks to the .NET library sending emails with PowerShell is surprisingly very simple!  One thing to remember is line breaks, when you need a new line, make sure to use the escape characters <i>`n</i> . In the first example, I&#8217;ll send just a regular email, in the second I&#8217;ll send a SMS text message.<span id="more-358"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$emailFrom</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;sender@example.com&quot;</span>
<span style="color: #800080;">$emailTo</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;recipient@example.com&quot;</span>
&nbsp;
<span style="color: #800080;">$subject</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Subject&quot;</span>
<span style="color: #800080;">$body</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;This is my automated email<span style="color: #008080; font-weight: bold;">`n</span>This is line number 2&quot;</span>
<span style="color: #800080;">$smtpServer</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;localhost&quot;</span>
&nbsp;
<span style="color: #800080;">$smtp</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-object</span> Net.Mail.SmtpClient<span style="color: #000000;">&#40;</span><span style="color: #800080;">$smtpServer</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$smtp</span>.Send<span style="color: #000000;">&#40;</span><span style="color: #800080;">$emailFrom</span><span style="color: pink;">,</span> <span style="color: #800080;">$emailTo</span><span style="color: pink;">,</span> <span style="color: #800080;">$subject</span><span style="color: pink;">,</span> <span style="color: #800080;">$body</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p>Now let&#8217;s send a SMS text massage alert, in this message we will include how much free space we have on our C: drive.  Most cell carriers limit text messages to 160 characters, so we want to keep them as short and useful as possible. Check the list below to see see what the email address of your phone is and make sure to replace the <i>$to</i> variable.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$freeSpace</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">gwmi</span> win32_logicaldisk <span style="color: pink;">-</span><span style="color: #0000FF;">filter</span> <span style="color: #800000;">&quot;DeviceID=<span style="color: #008080; font-weight: bold;">`&quot;</span>C:<span style="color: #008080; font-weight: bold;">`&quot;</span>&quot;</span> <span style="color: pink;">|</span> <span style="color: pink;">%</span> <span style="color: #000000;">&#123;</span> <span style="color: #000080;">$_</span>.freespace<span style="color: pink;">/</span>1GB <span style="color: #000000;">&#125;</span>
<span style="color: #800080;">$freeSpace</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>System.Math<span style="color: #000000;">&#93;</span>::round<span style="color: #000000;">&#40;</span><span style="color: #800080;">$FreeSpace</span><span style="color: pink;">,</span><span style="color: #804000;">2</span><span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #800080;">$from</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;from@example.com&quot;</span>
<span style="color: #800080;">$to</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;1234567890@example.com&quot;</span>
&nbsp;
<span style="color: #800080;">$subject</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;&quot;</span>
<span style="color: #800080;">$body</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;C: free space is &quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$freeSpace</span> <span style="color: pink;">+</span> <span style="color: #800000;">&quot; GB&quot;</span>
&nbsp;
<span style="color: #800080;">$smtpServer</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;localhost&quot;</span>
<span style="color: #800080;">$smtp</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> Net.Mail.SmtpClient<span style="color: #000000;">&#40;</span><span style="color: #800080;">$smtpServer</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$smtp</span>.Send<span style="color: #000000;">&#40;</span><span style="color: #800080;">$from</span><span style="color: pink;">,</span> <span style="color: #800080;">$to</span><span style="color: pink;">,</span> <span style="color: #800080;">$subject</span><span style="color: pink;">,</span> <span style="color: #800080;">$body</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p>Now we need to figure out what email address to use so that we can send the SMS address, below are a few examples for several US carriers.  Make sure to replace 1234567890 with your ten digit phone number.</p>
<p>Alltel<br />
1234567890@message.alltel.com</p>
<p>AT&#038;T<br />
1234567890@txt.att.net</p>
<p>Boost Mobile<br />
1234567890@myboostmobile.com</p>
<p>MetroPCS<br />
1234567890@mymetropcs.com</p>
<p>Nextel<br />
1234567890@messaging.nextel.com</p>
<p>Sprint PCS<br />
1234567890@messaging.sprintpcs.com</p>
<p>T-Mobile<br />
1234567890@tmomail.net</p>
<p>Verizon<br />
1234567890@vtext.com</p>
<p>Virgin Mobile<br />
1234567890@vmobl.com</p>
]]></content:encoded>
			<wfw:commentRss>http://www.brangle.com/wordpress/2009/08/send-email-messages-with-powershell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Append Text to a File Using Add-Content in PowerShell</title>
		<link>http://www.brangle.com/wordpress/2009/08/append-text-to-a-file-using-add-content-in-powershell/</link>
		<comments>http://www.brangle.com/wordpress/2009/08/append-text-to-a-file-using-add-content-in-powershell/#comments</comments>
		<pubDate>Sun, 23 Aug 2009 23:28:10 +0000</pubDate>
		<dc:creator>lopezg</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Windows PowerShell]]></category>

		<guid isPermaLink="false">http://www.brangle.com/wordpress/?p=321</guid>
		<description><![CDATA[Here's a quick tutorial on how to add text to the end of a text file using the <i>Add-Content</i> or its alias <i>ac</i> in PowerShell. In this first example let's add &#34;This is the last line&#34; to the end of a file. [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a quick tutorial on how to add text to the end of a text file using the <i>Add-Content</i> or its alias <i>ac</i> in PowerShell. In this first example let&#8217;s add &quot;This is the last line&quot; to the end of a file.<span id="more-321"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Add-Content</span> c:\sample.txt <span style="color: #800000;">&quot;This is the last line&quot;</span></pre></td></tr></table></div>

<p>  The example above adds the text to the last line, it doesn&#8217;t actually create a new line, so let&#8217;s try it again, and make sure that this time the text that we concatenate is on the last line all by itself.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Add-Content</span> c:\sample.txt <span style="color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">`r</span><span style="color: #008080; font-weight: bold;">`n</span>This is the last line&quot;</span></pre></td></tr></table></div>

<p>  You&#8217;ll notice a <i>`r`n</i> this is an escape character to tell PowerShell to add a carriage return and a new line, you&#8217;ll need this when working with Windows file.  If your going to move this file to a unix or linux computer, you&#8217;ll only need a <i>`n</i> there&#8217;s no need for the carriage return.</p>
<p>Here is a list of escape characters:</p>
<ul>
<li>`0  &#8212; Null
<li>`a  &#8212; Alert
<li>
<li>`b &#8212; Backspace
<li>`n &#8212; New line
<li>`r &#8212; Carriage return
<li>`t &#8212; Horizontal tab
<li>`&#8217; &#8212; Single quote
<li>`&#8221; &#8212; Double quote</ul>
<p>The example I gave above was the quick and dirty, we should really explicitly state our arguments and use quotes around the path, to do that we will add a path and a value argument</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800000;">&quot;c:\sample.txt&quot;</span> <span style="color: #008080; font-style: italic;">-Value</span> <span style="color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">`r</span><span style="color: #008080; font-weight: bold;">`n</span>This is the last line&quot;</span></pre></td></tr></table></div>

<p>Here&#8217;s a useful example, suppose you want to map hostname to a particular IP address.  Let&#8217;s map example.com to point to 127.0.0.1, we&#8217;ll do this using the <i>ac</i> alias for <i>Add-Content</i>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">ac</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800000;">&quot;C:\WINDOWS\system32\drivers\etc\hosts&quot;</span> <span style="color: #008080; font-style: italic;">-Value</span> <span style="color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">`r</span><span style="color: #008080; font-weight: bold;">`n</span>127.0.0.1    example.com&quot;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.brangle.com/wordpress/2009/08/append-text-to-a-file-using-add-content-in-powershell/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Pass Credentials via PowerShell</title>
		<link>http://www.brangle.com/wordpress/2009/08/pass-credentials-via-powershell/</link>
		<comments>http://www.brangle.com/wordpress/2009/08/pass-credentials-via-powershell/#comments</comments>
		<pubDate>Sun, 23 Aug 2009 20:33:07 +0000</pubDate>
		<dc:creator>lopezg</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Windows Management Instrumentation (WMI)]]></category>
		<category><![CDATA[Windows PowerShell]]></category>

		<guid isPermaLink="false">http://www.brangle.com/wordpress/?p=269</guid>
		<description><![CDATA[I've often found it necessary to pass my Administrator domain or Domain Admin (DomAdmin) credentials to run a PowerShell WMI command. Fortunately PowerShell provides 3 different ways of doing just that, not counting using Windows built in runas command. The three methods I'm going to go over are <ol><li>Entering your own unique credentials</li> <li>Entering a service account password, ie: the same username (or hard coding a username) <li>Hard coding a user name and password</li></ol> [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve often found it necessary to pass my Administrator domain or Domain Admin (DomAdmin) credentials to run a PowerShell WMI command.  Fortunately PowerShell provides 3 different ways of doing just that, not counting using Windows built in runas command.  The three methods I&#8217;m going to go over are</p>
<ol>
<li>Entering your own unique credentials</li>
<li>Entering a service account password, ie: the same username (or hard coding a username)
<li>Hard coding a user name and password</li>
</ol>
<p><span id="more-269"></span></p>
<p><br /><br /></p>
<p><b>Method 1:</b><div id="attachment_297" class="wp-caption alignleft" style="width: 310px"><a href="http://www.brangle.com/wordpress/wp-content/uploads/2009/08/WindowsPowerShellCredentialsRequest.jpg"><img src="http://www.brangle.com/wordpress/wp-content/uploads/2009/08/WindowsPowerShellCredentialsRequest-300x233.jpg" alt="Windows PowerShell Credentials Request Box" title="Windows PowerShell Credentials Request" width="300" height="233" class="size-medium wp-image-297" /></a><p class="wp-caption-text">Windows PowerShell Credentials Request Box</p></div> Entering your own unique credentials, this will popup the standard windows login, let&#8217;s store our credentials to a variable called <i>$cred</i>, the benefits of doing this if we want to run multiple commands all under this account we can just recall our credentials without having to enter them every single time.  In this example, after I retrieve my stored credentials I&#8217;ll check to see who is logged onto a computer named dev-ghost.  I&#8217;ll be using <i>gwmi</i> cmdlet which is just an alias for the <i>Get-WmiObject</i> cmdlet.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">PS</span> C:\<span style="color: pink;">&gt;</span> <span style="color: #800080;">$cred</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Credential</span>
&nbsp;
cmdlet <span style="color: #008080; font-weight: bold;">Get-Credential</span> at command pipeline position <span style="color: #804000;">1</span>
Supply values <span style="color: #0000FF;">for</span> the following parameters:
Credential
<span style="color: #008080; font-weight: bold;">PS</span> C:\<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">gwmi</span> win32_LoggedOnUser <span style="color: pink;">-</span>computer <span style="color: #800000;">&quot;dev-ghost&quot;</span> <span style="color: #008080; font-style: italic;">-credential</span> $cred</pre></td></tr></table></div>

<p><br /><br /><br />
<b>Method 2</b><br />
In this example, we are going to do the same as above, except we will specify the username, this is perfect for whenever you use service accounts.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">PS</span> C:\<span style="color: pink;">&gt;</span>PS C:\<span style="color: pink;">&gt;</span> <span style="color: #800080;">$cred</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Credential</span> <span style="color: #800000;">&quot;dev-ghost\administrator&quot;</span>
<span style="color: #008080; font-weight: bold;">PS</span> C:\<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">gwmi</span> win32_LoggedOnUser <span style="color: pink;">-</span>computer <span style="color: #800000;">&quot;dev-ghost&quot;</span> <span style="color: #008080; font-style: italic;">-credential</span> $cred</pre></td></tr></table></div>

<p>Now let&#8217;s do the same as above, but instead of saving our credentials to a variable, we will just use it once and call it inline.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">PS</span> C:\<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">gwmi</span> win32_LoggedOnUser <span style="color: pink;">-</span>computer <span style="color: #800000;">&quot;dev-ghost&quot;</span> <span style="color: #008080; font-style: italic;">-credential</span> <span style="color: #800000;">&quot;dev-ghost\administrator&quot;</span></pre></td></tr></table></div>

<p><br /><br /><br />
<b>Method 3</b><br />
I don&#8217;t think password should ever be hard coded into scripts, especially ones that can be decrypted.  PowerShell provides a SecureString, this is nothing more than a joke. The reason I call it a joke is because anyone can decrypt a SecureString if they know a little bit of PS.  But ignorance is bliss right? So let&#8217;s get to it&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #008080; font-weight: bold;">PS</span> C:\windows\system32\windowspowershell\v1.0<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">Read-Host</span> <span style="color: #008080; font-style: italic;">-AsSecureString</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">ConvertFrom-SecureString</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Out<span style="color: #FF0000;">-File</span></span> C:\PowerShell\MyPassword.txt</pre></td></tr></table></div>

<p>Once you press enter just type in your password, this will save your password to a file called MyPassword.txt. So now that the password is saved as a &quot;SecureString&quot; we can now use these saved credentials.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$password</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">type</span> C:\PowerShell\MyPassword.txt <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">ConvertTo-SecureString</span>
<span style="color: #800080;">$cred</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: #008080; font-style: italic;">-TypeName</span> System.Management.Automation.PSCredential <span style="color: #008080; font-style: italic;">-argumentlist</span> “administrator”<span style="color: pink;">,</span>$password</pre></td></tr></table></div>

<p> and that&#8217;s it, your all set to start impersonating your credentials so that you can run scripts or executable under your service accounts.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.brangle.com/wordpress/2009/08/pass-credentials-via-powershell/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Mounting a Virtual/Network Drive with PowerShell</title>
		<link>http://www.brangle.com/wordpress/2009/08/mounting-a-virtual-network-drive-with-powershell/</link>
		<comments>http://www.brangle.com/wordpress/2009/08/mounting-a-virtual-network-drive-with-powershell/#comments</comments>
		<pubDate>Fri, 14 Aug 2009 06:18:45 +0000</pubDate>
		<dc:creator>lopezg</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Windows PowerShell]]></category>

		<guid isPermaLink="false">http://www.brangle.com/wordpress/?p=251</guid>
		<description><![CDATA[There's multiple reasons why someone would want to mount a virtual or network drive, especially in an enterprise environment where many users don't understand the concept of UNC paths or when older software doesn't support long path names or UNC connections. Now there's two things we need to know, is this a local path, or is it somewhere on the network. Once we know that we can get started. [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s multiple reasons why someone would want to mount a virtual or network drive, especially in an enterprise environment where many users don&#8217;t understand the concept of UNC paths or when older software doesn&#8217;t support long path names or UNC connections.  Now there&#8217;s two things we need to know, is this a local path, or is it somewhere on the network.  Once we know that we can get started.<span id="more-251"></span></p>
<p>If the folder you want to mount happens to exist on the same computer, i.e. locally we can use the subst.exe command.  Let&#8217;s assume we want to mount the administrators photos to the drive letter P: we should run the following in the command prompt:</p>
<pre>subst P: "C:\Documents and Settings\Administrator\My Documents\My Pictures"</pre>
<p>Do not put a trailing slash at the end of Pictures, subst.exe doesn&#8217;t like it and will complain that it can&#8217;t find the path. Of course you don&#8217;t even need to run this command from the command prompt, you could run it from the Run menu.  In general the command to create a virtual drive is</p>
<pre>subst [drive1:] [drive2:]path]</pre>
<p> where drive1 is the drive letter that you want to create and drive2:path is the file location of the folder you want to map.</p>
<p>Ok, so now we&#8217;v created the virtual drive and we&#8217;re ready to get rid of it. This procedure isn&#8217;t as easy as right clicking on the drive and selecting Disconnect, after all you can&#8217;t disconnect if it&#8217;s a local folder. You&#8217;re going to have to delete the virtual drive using the delete switch /d</p>
<pre>subst P: /d</pre>
<p> It really can&#8217;t get any simplier.</p>
<p>Ok, so now let&#8217;s try mapping a UNC drive with PowerShell. It&#8217;s just that simple.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$drive</span> <span style="color: pink;">=</span> $<span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">New-Object</span> <span style="color: pink;">-</span>Com WScript.Network<span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$drive</span>.MapNetworkDrive<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;w:&quot;</span><span style="color: pink;">,</span> <span style="color: #800000;">&quot;\\ComputerName\Share&quot;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.brangle.com/wordpress/2009/08/mounting-a-virtual-network-drive-with-powershell/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

