<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7774240277251981464</id><updated>2012-01-03T17:10:45.252+05:30</updated><category term='Unix find equivalent'/><category term='Regex'/><category term='Set-ExecutionPolicy'/><category term='AWK Equivalent'/><category term='Registry'/><category term='cd -'/><category term='Find-ChildItem'/><category term='grep'/><category term='wc'/><title type='text'>Windows Powershell</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://windows-powershell-scripts.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7774240277251981464/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://windows-powershell-scripts.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Jagadish</name><uri>http://www.blogger.com/profile/18242041135736190793</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='29' src='http://1.bp.blogspot.com/_cBBGhuN8esk/SebErT0XphI/AAAAAAAAAEE/kNhdrJF4928/S220/DSC00119.JPG'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>8</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7774240277251981464.post-626216934726177696</id><published>2009-08-08T13:44:00.098+05:30</published><updated>2009-11-07T12:00:41.200+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Unix find equivalent'/><category scheme='http://www.blogger.com/atom/ns#' term='Find-ChildItem'/><title type='text'>Unix / Linux find equivalent in Powershell Find-ChildItem Cmdlet</title><content type='html'>Find is one of the most often used commands in day to day work life. Unix find command provides lot of features and options that enable users to find files/directories more effectively&lt;br /&gt;&lt;br /&gt;In Powershell, we can use Get-ChildItem cmdlet to search for files. But it doesn't give you all the fun. So I have developed &lt;b&gt;Find-ChildItem&lt;/b&gt; Powershell cmdlet which is equivalent to Unix find command.&lt;br /&gt;&lt;br /&gt;&lt;table&gt;&lt;tr&gt;&lt;td&gt;&lt;a style="text-decoration:none" href="http://cid-66f71555c2c0911c.skydrive.live.com/self.aspx/.Public/Cmdlets/Find-ChildItem.zip" onclick="pageTracker._setVar('findchilditem');"&gt; &lt;img src="http://lh6.ggpht.com/_cBBGhuN8esk/Sn03o5FxWiI/AAAAAAAAAHM/zANxfESuFqQ/s144/download.gif" height="30" width="30" style="border-style: none" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;td&gt;Download Find-ChildItem for both 32 bits and 64 bits&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;&lt;span style='color:red;'&gt;&lt;b&gt;Note:&lt;/b&gt; For 64 bit operating system, you will have to use different &lt;b&gt;InstallUtil.exe&lt;/b&gt;. See below for details&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Syntax:&lt;pre&gt;Find-ChildItem [-Name &amp;lt;String&amp;gt;] [-Type &amp;lt;String&amp;gt;] [-MaxDepth &amp;lt;Int32&amp;gt;] [-MinDepth &amp;lt;Int32&amp;gt;] [-CMin &amp;lt;String&amp;gt;]&lt;br /&gt;[-AMin &amp;lt;String&amp;gt;] [-WMin &amp;lt;String&amp;gt;] [-CTime &amp;lt;String&amp;gt;] [-ATime &amp;lt;String&amp;gt;] [-WTime &amp;lt;String&amp;gt;] [-Size &amp;lt;String&amp;gt;]&lt;br /&gt;[-Empty] [-Exec &amp;lt;String&amp;gt;] [-Delete] [-OutObject] [[-Path] &amp;lt;String&amp;gt;] [&amp;lt;CommonParameters&amp;gt;]&lt;br /&gt;&lt;/pre&gt;&lt;/b&gt;&lt;br /&gt;&lt;table class="tablestyle"&gt;&lt;tr class="tablehed"&gt;&lt;td width=150&gt;Parameter&lt;/td&gt;&lt;td&gt;Description&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Name&lt;/td&gt;&lt;td&gt;Accepts .Net regular expression to match FileName&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Type&lt;/td&gt;&lt;td&gt;d - directory, f - file&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;MaxDepth&lt;/td&gt;&lt;td&gt;Descend at most levels (a non-negative integer) levels of directories below the command line arguments&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;MinDepth&lt;/td&gt;&lt;td&gt;Do not apply any tests or actions at levels less than specified mindepth level  (a non-negative  integer)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;CMin&lt;/td&gt;&lt;td&gt;File was created n minutes ago&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;CTime&lt;/td&gt;&lt;td&gt;File was created n*24 hours ago&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;AMin&lt;/td&gt;&lt;td&gt;File was last accessed n minutes ago&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;ATime&lt;/td&gt;&lt;td&gt;File was last accessed n*24 hours ago&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;WMin&lt;/td&gt;&lt;td&gt;File was last changed/written n minutes ago&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;WTime&lt;/td&gt;&lt;td&gt;File was last changed/written n*24 hours ago&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Size&lt;/td&gt;&lt;td&gt;File uses n units of space&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Empty&lt;/td&gt;&lt;td&gt;File is empty&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Exec&lt;/td&gt;&lt;td&gt;Execute Cmdlets in the given argument.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Delete&lt;/td&gt;&lt;td&gt;Delete file or directory&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;OutObject&lt;/td&gt;&lt;td&gt;Write Objects in output instead of just Directory/File Names&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;&lt;u&gt;&lt;b&gt;Find-ChildItem Cmdlet Installation Instructions:&lt;/b&gt;&lt;/u&gt;&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Extract Find-ChildItem.zip files to C:\Windows\Microsoft.NET\Framework\ Directory&lt;li&gt;Open Powershell with Administrator Privileges&lt;li&gt;Now, lets install the cmdlet using InstallUtil.exe utility which is available as part of Microsoft Windows SDK.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;For 32 Bit Operating System:&lt;/b&gt;&lt;br /&gt;&lt;pre&gt;C:\Windows\Microsoft.NET\&lt;span style='color:red;'&gt;&lt;b&gt;Framework&lt;/b&gt;&lt;/span&gt;\v2.0.50727\InstallUtil.exe "C:\Windows\Microsoft.NET\&lt;br /&gt;Framework\Find-ChildItem.dll"&lt;/pre&gt;&lt;b&gt;For 64 Bit Operating System:&lt;/b&gt;&lt;br /&gt;&lt;pre&gt;C:\Windows\Microsoft.NET\&lt;span style='color:red;'&gt;&lt;b&gt;Framework64&lt;/b&gt;&lt;/span&gt;\v2.0.50727\InstallUtil.exe "C:\Windows\Microsoft.NET\&lt;br /&gt;Framework\Find-ChildItem.dll"&lt;/pre&gt;&lt;li&gt;Type &lt;b&gt;Get-PSSnapin -Registered&lt;/b&gt; and press enter. If it displays FindChildItemPSSnapIn01 SnapIn, then our installation is successful&lt;li&gt;Now, before we can use Find-ChildItem Cmdlet, we need to add FindChildItemPSSnapIn01 using &lt;b&gt;Add-PSSnapin FindChildItemPSSnapIn01&lt;/b&gt;&lt;li&gt;To permanently add the Snapin, &lt;b&gt;Add-PSSnapin FindChildItemPSSnapIn01&lt;/b&gt; Cmdlet need to be added to Windows Powershell Profile file&lt;li&gt;See the below window, to get clear picture on Cmdlet Installation on Windows 7 32 Bits&lt;/ol&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS C:\Users\Jagadish\&gt; cd "C:\Windows\Microsoft.NET\Framework"&lt;br /&gt;PS C:\Windows\Microsoft.NET\Framework&gt; Get-ChildItem .\Find-ChildItem*&lt;br /&gt;&lt;br /&gt;    Directory: C:\Windows\Microsoft.NET\Framework&lt;br /&gt;&lt;br /&gt;Mode                LastWriteTime     Length Name&lt;br /&gt;----                -------------     ------ ----&lt;br /&gt;-a---          6/8/2009   1:13 PM      12288 Find-ChildItem.dll&lt;br /&gt;-a---          6/8/2009   1:14 PM      21298 Find-ChildItem.dll-Help.xml&lt;br /&gt;&lt;br /&gt;PS C:\Windows\Microsoft.NET\Framework&gt; C:\Windows\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe .\Find-ChildItem.dll&lt;br /&gt;Microsoft (R) .NET Framework Installation utility Version 2.0.50727.3521&lt;br /&gt;Copyright (c) Microsoft Corporation.  All rights reserved.&lt;br /&gt;&lt;br /&gt;Running a transacted installation.&lt;br /&gt;   .&lt;br /&gt;   .&lt;br /&gt;   .&lt;br /&gt;The Commit phase completed successfully.&lt;br /&gt;&lt;br /&gt;The transacted install has completed.&lt;br /&gt;&lt;br /&gt;PS C:\Windows\Microsoft.NET\Framework&gt; Get-PSSnapin -Registered&lt;br /&gt;&lt;br /&gt;Name        : FindChildItemPSSnapIn01&lt;br /&gt;PSVersion   : 2.0&lt;br /&gt;Description : This is Powershell SnapIn that includes Find-ChildItem Cmdlet, developed by Jagadish &amp;lt;http://windows-powershell-scripts.blogspot.com/&amp;gt;&lt;br /&gt;&lt;br /&gt;PS C:\Windows\Microsoft.NET\Framework&gt; Add-PSSnapin FindChildItemPSSnapIn01&lt;br /&gt;&lt;br /&gt;PS C:\Windows\Microsoft.NET\Framework&gt; Find-ChildItem G:\Jagadish\Cmdlets&lt;br /&gt;G:\Jagadish\Cmdlets\Test&lt;br /&gt;G:\Jagadish\Cmdlets\Test\1&lt;br /&gt;G:\Jagadish\Cmdlets\Test\1\2&lt;br /&gt;G:\Jagadish\Cmdlets\Test\1\file2&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;If you don't have windows powershell profile file, follow the below steps to create a new one and add "Add-PSSnapin FindChildItemPSSnapIn01" entry to it&lt;br /&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS C:\Users\Jagadish\Desktop\Find-ChildItem&gt; New-Item $profile -ItemType file -Force ; Write-Output "Add-PSSnapin FindChildItemPSSnapIn01" &gt; $profile&lt;br /&gt;&lt;br /&gt;    Directory: C:\Users\Jagadish\Documents\WindowsPowerShell&lt;br /&gt;&lt;br /&gt;Mode                LastWriteTime     Length Name&lt;br /&gt;----                -------------     ------ ----&lt;br /&gt;-a---          6/8/2009   6:28 PM          0 Microsoft.PowerShell_profile.ps1&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;That's it. You added the Add-Snapin entry permanently. Now, Lets have some fun with new Find-ChildItem Cmdlet&lt;br /&gt;&lt;br /&gt;List all the files and directories in the current directory&lt;br /&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS G:\Jagadish\Test&gt; Find-ChildItem&lt;br /&gt;G:\Jagadish\Test\Docs&lt;br /&gt;G:\Jagadish\Test\Docs\101_Popular_Interview_Questions.doc&lt;br /&gt;G:\Jagadish\Test\Docs\echosend_1500.c&lt;br /&gt;G:\Jagadish\Test\Downloads&lt;br /&gt;G:\Jagadish\Test\Downloads\VLC&lt;br /&gt;G:\Jagadish\Test\Downloads\VLC\vlc-0.8.6-win32.exe&lt;br /&gt;G:\Jagadish\Test\Downloads\VLC\vlc_src-0.8.5.tar.gz&lt;br /&gt;G:\Jagadish\Test\Downloads\ethereal-setup-0.10.13.exe&lt;br /&gt;G:\Jagadish\Test\Downloads\TeamViewer_Setup.exe&lt;br /&gt;G:\Jagadish\Test\Songs&lt;br /&gt;G:\Jagadish\Test\Songs\Green Day&lt;br /&gt;G:\Jagadish\Test\Songs\Green Day\American Idiot- Boulevard Of Broken Dreams.mp3&lt;br /&gt;G:\Jagadish\Test\Songs\akon-sorry.mp3&lt;br /&gt;G:\Jagadish\Test\msvcp71.dll&lt;br /&gt;PS G:\Jagadish\Test&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;List ".exe" files only&lt;br /&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS G:\Jagadish\Test&gt; Find-ChildItem -Type f -Name ".*.exe"&lt;br /&gt;G:\Jagadish\Test\Downloads\VLC\vlc-0.8.6-win32.exe&lt;br /&gt;G:\Jagadish\Test\Downloads\ethereal-setup-0.10.13.exe&lt;br /&gt;G:\Jagadish\Test\Downloads\TeamViewer_Setup.exe&lt;br /&gt;PS G:\Jagadish\Test&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;Search for ".c" files only and display no of lines, words, characters in that file&lt;br /&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS G:\Jagadish\Test&gt; Find-ChildItem -Type f -Name "\.c$" -Exec "Get-Content {} | Measure-Object -Line -Character -Word"&lt;br /&gt;&lt;br /&gt;           Lines            Words       Characters Property&lt;br /&gt;           -----            -----       ---------- --------&lt;br /&gt;           162              500         3703&lt;br /&gt;&lt;br /&gt;PS G:\Jagadish\Test&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;You can use the below command to find Empty files in a directory and its sub directories&lt;br /&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS E:\Jagadish\Test&gt; Find-ChildItem -Type f -Empty&lt;br /&gt;E:\Jagadish\Test\Docs\Attachment&lt;br /&gt;E:\Jagadish\Test\Docs\Report.txt&lt;br /&gt;E:\Jagadish\Test\Docs\sample_logs&lt;br /&gt;E:\Jagadish\Test\testing&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;Use -OutObject parameter to write file objects to powershell&lt;br /&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS E:\Jagadish\Test&gt; Find-ChildItem -Type f -Empty -OutObject&lt;br /&gt;&lt;br /&gt;    Directory: Microsoft.PowerShell.Core\FileSystem::E:\Jagadish\Test\Docs&lt;br /&gt;&lt;br /&gt;Mode                LastWriteTime     Length Name&lt;br /&gt;----                -------------     ------ ----&lt;br /&gt;-a---         8/11/2009  10:16 PM          0 Attachment&lt;br /&gt;-a---         8/11/2009  10:16 PM          0 Report.txt&lt;br /&gt;-a---         8/11/2009  10:16 PM          0 sample_logs&lt;br /&gt;&lt;br /&gt;    Directory: Microsoft.PowerShell.Core\FileSystem::E:\Jagadish\Test&lt;br /&gt;&lt;br /&gt;Mode                LastWriteTime     Length Name&lt;br /&gt;----                -------------     ------ ----&lt;br /&gt;-a---         8/11/2009  10:16 PM          0 testing&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;Now, If we want to delete all the empty files, we can easily do that with the help of Find-ChildItem cmdlet&lt;br /&gt;&lt;code&gt;Find-ChildItem -Type f -Empty -Delete&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS E:\Jagadish\Test&gt; Find-ChildItem -Type f -Empty -Delete&lt;br /&gt;PS E:\Jagadish\Test&gt; Find-ChildItem -Type f -Empty -OutObject&lt;br /&gt;PS E:\Jagadish\Test&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;Now, list all the files with size greater than 9 MB&lt;br /&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS E:\Jagadish\Test&gt; Find-ChildItem -Type f -Size +9M -OutObject&lt;br /&gt;&lt;br /&gt;    Directory: Microsoft.PowerShell.Core\FileSystem::E:\Jagadish\Test\Downloads\VLC&lt;br /&gt;&lt;br /&gt;Mode                LastWriteTime     Length Name&lt;br /&gt;----                -------------     ------ ----&lt;br /&gt;-a---        12/13/2006   3:26 PM    9451515 vlc-0.8.6-win32.exe&lt;br /&gt;-a---         1/25/2007   3:00 PM   81510400 vlc-binary-fc4.tar.gz&lt;br /&gt;-a---          7/7/2006   5:46 PM   15680081 vlc-binary.tar.gz&lt;br /&gt;-a---         11/8/2006   5:45 PM   12461262 vlc_src-0.8.5.tar.gz&lt;br /&gt;&lt;br /&gt;    Directory: Microsoft.PowerShell.Core\FileSystem::E:\Jagadish\Test\Downloads&lt;br /&gt;&lt;br /&gt;Mode                LastWriteTime     Length Name&lt;br /&gt;----                -------------     ------ ----&lt;br /&gt;-a---         11/9/2006   7:24 PM   11348769 ethereal-setup-0.10.13.exe&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;If you want more details and usage information on this Find-ChildItem Cmdlet, type &lt;b&gt;Get-Help Find-ChildItem -Full&lt;/b&gt; in your Powershell&lt;br /&gt;&lt;br /&gt;Let me know if you need any other information or if you encounter any bugs/issues with Find-ChildItem Cmdlet&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7774240277251981464-626216934726177696?l=windows-powershell-scripts.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://windows-powershell-scripts.blogspot.com/feeds/626216934726177696/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://windows-powershell-scripts.blogspot.com/2009/08/unix-linux-find-equivalent-in.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7774240277251981464/posts/default/626216934726177696'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7774240277251981464/posts/default/626216934726177696'/><link rel='alternate' type='text/html' href='http://windows-powershell-scripts.blogspot.com/2009/08/unix-linux-find-equivalent-in.html' title='Unix / Linux find equivalent in Powershell Find-ChildItem Cmdlet'/><author><name>Jagadish</name><uri>http://www.blogger.com/profile/18242041135736190793</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='29' src='http://1.bp.blogspot.com/_cBBGhuN8esk/SebErT0XphI/AAAAAAAAAEE/kNhdrJF4928/S220/DSC00119.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh6.ggpht.com/_cBBGhuN8esk/Sn03o5FxWiI/AAAAAAAAAHM/zANxfESuFqQ/s72-c/download.gif' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7774240277251981464.post-4878557435428630697</id><published>2009-07-04T14:27:00.001+05:30</published><updated>2009-08-11T20:21:38.695+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='cd -'/><title type='text'>Change to Previous Working Directory in Powershell (cd -)</title><content type='html'>One of the cool features of linux bash is the ability to go to the previous working directory. In Bash, we can go to the previous working directory using "cd -" command. &lt;br /&gt;&lt;br /&gt;Unfortunately, we do not have an equivalent of "cd -" in Powershell. So I just wrote a simple powershell cd function for this cool feature&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Powershell CD function:&lt;/b&gt;&lt;br /&gt;&lt;table class="codewindow" cellpadding=2&gt;&lt;tr class=d0&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;function &lt;span class=syn_keyword&gt;cd&lt;/span&gt; {&lt;/td&gt;&lt;/tr&gt;&lt;tr class=&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr class=d0&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;    &lt;span class=syn_keyword&gt;if&lt;/span&gt; (&lt;span class=syn_var&gt;$args[0]&lt;/span&gt; -eq &lt;span class=syn_string&gt;'-'&lt;/span&gt;)&lt;span class=syn_param&gt;&lt;/span&gt; {&lt;/td&gt;&lt;/tr&gt;&lt;tr class=&gt;&lt;td&gt;4&lt;/td&gt;&lt;td&gt;        &lt;span class=syn_var&gt;$pwd&lt;/span&gt;=&lt;span class=syn_var&gt;$OLDPWD&lt;/span&gt;;&lt;/td&gt;&lt;/tr&gt;&lt;tr class=d0&gt;&lt;td&gt;5&lt;/td&gt;&lt;td&gt;    } &lt;span class=syn_keyword&gt;else&lt;/span&gt; {&lt;/td&gt;&lt;/tr&gt;&lt;tr class=&gt;&lt;td&gt;6&lt;/td&gt;&lt;td&gt;        &lt;span class=syn_var&gt;$pwd&lt;/span&gt;=&lt;span class=syn_var&gt;$args[0]&lt;/span&gt;;&lt;/td&gt;&lt;/tr&gt;&lt;tr class=d0&gt;&lt;td&gt;7&lt;/td&gt;&lt;td&gt;    }&lt;/td&gt;&lt;/tr&gt;&lt;tr class=&gt;&lt;td&gt;8&lt;/td&gt;&lt;td&gt;    &lt;/td&gt;&lt;/tr&gt;&lt;tr class=d0&gt;&lt;td&gt;9&lt;/td&gt;&lt;td&gt;    &lt;span class=syn_var&gt;$tmp&lt;/span&gt;=&lt;span class=syn_keyword&gt;pwd&lt;/span&gt;;&lt;/td&gt;&lt;/tr&gt;&lt;tr class=&gt;&lt;td&gt;10&lt;/td&gt;&lt;td&gt;    &lt;/td&gt;&lt;/tr&gt;&lt;tr class=d0&gt;&lt;td&gt;11&lt;/td&gt;&lt;td&gt;    &lt;span class=syn_keyword&gt;if&lt;/span&gt; (&lt;span class=syn_var&gt;$pwd&lt;/span&gt;) {&lt;/td&gt;&lt;/tr&gt;&lt;tr class=&gt;&lt;td&gt;12&lt;/td&gt;&lt;td&gt;        &lt;span class=syn_cmdlet&gt;Set-Location&lt;/span&gt; &lt;span class=syn_var&gt;$pwd&lt;/span&gt;;&lt;/td&gt;&lt;/tr&gt;&lt;tr class=d0&gt;&lt;td&gt;13&lt;/td&gt;&lt;td&gt;    }&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr class=&gt;&lt;td&gt;14&lt;/td&gt;&lt;td&gt;    &lt;/td&gt;&lt;/tr&gt;&lt;tr class=d0&gt;&lt;td&gt;15&lt;/td&gt;&lt;td&gt;    &lt;span class=syn_cmdlet&gt;Set-Variable&lt;/span&gt; -Name &lt;span class=syn_param&gt;OLDPWD&lt;/span&gt; -Value &lt;span class=syn_var&gt;$tmp&lt;/span&gt; -Scope &lt;span class=syn_param&gt;global&lt;/span&gt;;&lt;/td&gt;&lt;/tr&gt;&lt;tr class=&gt;&lt;td&gt;16&lt;/td&gt;&lt;td&gt;}&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;Now lets see, how to install the above function in your powershell&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Step 1:&lt;/b&gt; Remove Alias for &lt;b&gt;cd&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;b&gt;cd&lt;/b&gt; is actually not a powershell cmdlet. It is an &lt;b&gt;*alias*&lt;/b&gt; to Set-Location cmdlet. To install our new cd function, first we need to remove this alias&lt;br /&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS C:\Scripts&gt; Get-Command cd&lt;br /&gt;&lt;br /&gt;CommandType     Name                                                Definition&lt;br /&gt;-----------     ----                                                ----------&lt;br /&gt;Alias           cd                                                  Set-Location&lt;br /&gt;&lt;br /&gt;PS C:\Scripts&gt; Remove-Item Alias:cd&lt;br /&gt;PS C:\Scripts&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;Step 2:&lt;/b&gt; Install Powershell cd function&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;We have removed the alias for cd. Now we can just go ahead and install our new powershell cd function which is very easy. Just paste the complete function in one line.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Powershell CD function in one line:&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style='overflow: auto;background-color:white; border-width:1px; border-color:lightgray; border-style:solid; font-size:12px; font-family: Consolas, "Courier New" , Courier, mono; font-weight: bold; color:black; white-space: pre;'&gt;&lt;br /&gt;function &lt;span class=syn_keyword&gt;cd&lt;/span&gt; { &lt;span class=syn_keyword&gt;if&lt;/span&gt; (&lt;span class=syn_var&gt;$args[0]&lt;/span&gt; -eq &lt;span class=syn_string&gt;'-'&lt;/span&gt;)&lt;span class=syn_param&gt;&lt;/span&gt; { &lt;span class=syn_var&gt;$pwd&lt;/span&gt;=&lt;span class=syn_var&gt;$OLDPWD&lt;/span&gt;; } &lt;span class=syn_keyword&gt;else&lt;/span&gt; { &lt;span class=syn_var&gt;$pwd&lt;/span&gt;=&lt;span class=syn_var&gt;$args[0]&lt;/span&gt;; } &lt;span class=syn_var&gt;$tmp&lt;/span&gt;=&lt;span class=syn_keyword&gt;pwd&lt;/span&gt;; &lt;span class=syn_keyword&gt;if&lt;/span&gt; (&lt;span class=syn_var&gt;$pwd&lt;/span&gt;) { &lt;span class=syn_cmdlet&gt;Set-Location&lt;/span&gt; &lt;span class=syn_var&gt;$pwd&lt;/span&gt;; } &lt;span class=syn_cmdlet&gt;Set-Variable&lt;/span&gt; -Name &lt;span class=syn_param&gt;OLDPWD&lt;/span&gt; -Value &lt;span class=syn_var&gt;$tmp&lt;/span&gt; -Scope &lt;span class=syn_param&gt;global&lt;/span&gt;; }&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS C:\Scripts&gt; function cd { if ($args[0] -eq '-') { $pwd=$OLDPWD; } else { $pwd=$args[0]; } $tmp=pwd; if ($pwd) { Set-Location $pwd; } Set-Variable -Name OLDPWD -Value $tmp -Scope global; }&lt;br /&gt;PS C:\Scripts&gt;&lt;br /&gt;PS C:\Scripts&gt; Get-Item function:cd&lt;br /&gt;&lt;br /&gt;CommandType     Name                                                Definition&lt;br /&gt;-----------     ----                                                ----------&lt;br /&gt;Function        cd                                                   if ($args[0] -eq '-') { $pwd=$OLDPWD; } else { ...&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;Ready to go&lt;/b&gt;, Lets see this cool feature in action&lt;br /&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS C:\Scripts&gt; cd C:\Users\Jagadish\Desktop&lt;br /&gt;PS C:\Users\Jagadish\Desktop&gt; cd -&lt;br /&gt;PS C:\Scripts&gt; cd -&lt;br /&gt;PS C:\Users\Jagadish\Desktop&gt; cd ..\Documents&lt;br /&gt;PS C:\Users\Jagadish\Documents&gt; cd C:\Scripts&lt;br /&gt;PS C:\Scripts&gt; dir&lt;br /&gt;&lt;br /&gt;    Directory: C:\Scripts&lt;br /&gt;&lt;br /&gt;Mode                LastWriteTime     Length Name&lt;br /&gt;----                -------------     ------ ----&lt;br /&gt;-a---          6/6/2009  12:14 PM        279 test.ps1&lt;br /&gt;-a---         6/20/2009   8:27 PM         69 test1.ps1&lt;br /&gt;&lt;br /&gt;PS C:\Scripts&gt; cd -&lt;br /&gt;PS C:\Users\Jagadish\Documents&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;Hope you like it :)&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Note:&lt;/b&gt; Powershell will not save any functions/aliases by default. So if you close your powershell and reopen it, this function will not be available. To make it permanent, it should be added in the &lt;b&gt;Windows Powershell Profile&lt;/b&gt; file&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7774240277251981464-4878557435428630697?l=windows-powershell-scripts.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://windows-powershell-scripts.blogspot.com/feeds/4878557435428630697/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://windows-powershell-scripts.blogspot.com/2009/07/cd-change-to-previous-working-directory.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7774240277251981464/posts/default/4878557435428630697'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7774240277251981464/posts/default/4878557435428630697'/><link rel='alternate' type='text/html' href='http://windows-powershell-scripts.blogspot.com/2009/07/cd-change-to-previous-working-directory.html' title='Change to Previous Working Directory in Powershell (cd -)'/><author><name>Jagadish</name><uri>http://www.blogger.com/profile/18242041135736190793</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='29' src='http://1.bp.blogspot.com/_cBBGhuN8esk/SebErT0XphI/AAAAAAAAAEE/kNhdrJF4928/S220/DSC00119.JPG'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7774240277251981464.post-6309733130664605882</id><published>2009-06-20T02:40:00.011+05:30</published><updated>2009-10-23T09:52:53.903+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='wc'/><category scheme='http://www.blogger.com/atom/ns#' term='grep'/><title type='text'>Unix Equivalents in Powershell</title><content type='html'>I come from Linux background which I really like &lt;img src="http://lh4.ggpht.com/_cBBGhuN8esk/SjwHE6YsRaI/AAAAAAAAAFw/zI2jI-lw_O0/s800/8.gif" height="25" width="25" style="border-style: none"/&gt;. There are lot more utilities in Unix/Linux which provide more convenient way of doing some work. They are really very useful in speeding up the work and automating repetitive tasks.&lt;br /&gt;&lt;br /&gt;Since the release of Windows Powershell, it has proven to be one of the most powerful shells available now.&lt;br /&gt;&lt;br /&gt;As I started using Powershell, I find myself most of the times searching for UNIX equivalents in windows powershell. Here are a few which i came across&lt;br /&gt;&lt;br /&gt;&lt;b&gt;wc in Powershell: Measure-Object&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="bashdivstyle"&gt;&lt;br /&gt;[jagadish.g@localhost Scripts]$ cat test.csv | wc&lt;br /&gt;      4      13     277&lt;br /&gt;[jagadish.g@localhost Scripts]$ cat test.csv | wc -l&lt;br /&gt;4&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS C:\Scripts&gt; Get-Content test.csv | Measure-Object -line -word -character&lt;br /&gt;&lt;br /&gt;         Lines         Words         Characters Property&lt;br /&gt;         -----         -----         ---------- --------&lt;br /&gt;         4             13            273&lt;br /&gt;&lt;br /&gt;PS C:\Scripts&gt; Get-Content test.csv | Measure-Object -line&lt;br /&gt;&lt;br /&gt;         Lines Words         Characters         Property&lt;br /&gt;         ----- -----         ----------         --------&lt;br /&gt;         4&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;time in Powershell: Measure-Command&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="bashdivstyle"&gt;&lt;br /&gt;[jagadish.g@localhost Scripts]$ time sleep 5&lt;br /&gt;&lt;br /&gt;real    0m5.003s&lt;br /&gt;user    0m0.000s&lt;br /&gt;sys     0m0.001s&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS C:\Scripts&gt; Measure-Command { Sleep 5 }&lt;br /&gt;&lt;br /&gt;Days              : 0&lt;br /&gt;Hours             : 0&lt;br /&gt;Minutes           : 0&lt;br /&gt;Seconds           : 4&lt;br /&gt;Milliseconds      : 999&lt;br /&gt;Ticks             : 49996681&lt;br /&gt;TotalDays         : 5.78665289351852E-05&lt;br /&gt;TotalHours        : 0.00138879669444444&lt;br /&gt;TotalMinutes      : 0.0833278016666667&lt;br /&gt;TotalSeconds      : 4.9996681&lt;br /&gt;TotalMilliseconds : 4999.6681&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;!--&lt;b&gt;Find equivalent in Powershell&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Find is one of the most often used command in day to day work life. Unix find command provides lot of features and options that enable users to find files/directories more effectively&lt;br /&gt;&lt;br /&gt;In Powershell, we can use Get-ChildItem cmdlet to search files. But it doesn't give you all the fun. So I have developed Find-ChildItem Powershell cmdlet which is equivalent to Unix find command. &lt;a href="http://windows-powershell-scripts.blogspot.com/2009/08/unix-linux-find-equivalent-in.html"&gt;read more...&lt;/a&gt;&lt;br /&gt;--&gt;&lt;b&gt;GREP in Powershell: Select-String&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="bashdivstyle"&gt;&lt;br /&gt;[jagadish.g@localhost Scripts]$ cat test.csv | grep "Tony Passaquale"&lt;br /&gt;1,Tony Passaquale,7920,20090222 21:59:00,800,4.78,3824,Follow-up&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS C:\Scripts&gt; Get-Content test.csv | Select-String "Tony Passaquale"&lt;br /&gt;&lt;br /&gt;1,Tony Passaquale,7920,20090222 21:59:00,800,4.78,3824,Follow-up&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;NOTE, Select-String is Case-Insensitive by default. Use -CaseSensitive switch parameter to make it case sensitive.&lt;br /&gt;&lt;br /&gt;Now lets try to get one line after the line with the match&lt;br /&gt;&lt;br /&gt;&lt;div class="bashdivstyle"&gt;&lt;br /&gt;[jagadish.g@localhost Scripts]$ cat test.csv | grep -A1 "Tony Passaquale"&lt;br /&gt;1,Tony Passaquale,7920,20090222 21:59:00,800,4.78,3824,Follow-up&lt;br /&gt;2,Nigel Shan Shanford,30316,20090405 16:34:00,400,9.99,3996,New-Opportunity&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS C:\Scripts&gt; Get-Content test.csv | Select-String "Tony Passaquale" -Context 0,1&lt;br /&gt;&lt;br /&gt;&gt; 1,Tony Passaquale,7920,20090222 21:59:00,800,4.78,3824,Follow-up&lt;br /&gt;  2,Nigel Shan Shanford,30316,20090405 16:34:00,400,9.99,3996,New-Opportunity&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;The -Context parameter in the Select-String cmdlet is used to capture the specified number of lines before and after the line with the match. If you look at the above code window, I have passed '0,1' value to the -Context parameter. The first integer specifies the number of lines to print before the match (in this case, it is 0) and the next integer specifies the number of lines to print after the match (in this case, it is 1)&lt;br /&gt;&lt;br /&gt;Select-String cmdlet is using regular expression matching by default. So you can use use regex patterns to select the required lines.&lt;br /&gt;&lt;br /&gt;&lt;div class="bashdivstyle"&gt;&lt;br /&gt;[jagadish.g@localhost Scripts]$ cat test.csv | grep "y$"&lt;br /&gt;2,Nigel Shan Shanford,30316,20090405 16:34:00,400,9.99,3996,New-Opportunity&lt;br /&gt;4,Allen James,95140,20090405 16:31:00,1000,9.99,9990,New-Opportunity&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS C:\Scripts&gt; Get-Content test.csv | Select-String "y$"&lt;br /&gt;&lt;br /&gt;2,Nigel Shan Shanford,30316,20090405 16:34:00,400,9.99,3996,New-Opportunity&lt;br /&gt;4,Allen James,95140,20090405 16:31:00,1000,9.99,9990,New-Opportunity&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;AWK in Powershell&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Powershell has lot of features and abilities for text parsing. AWK is one of very powerful commands available for text parsing in Unix/Linux. We do not have a Awk like cmdlet in Powershell. But we can do everything in Powershell that can be done with Awk.&lt;br /&gt;&lt;br /&gt;Powershell combined with .Net Classes provide very powerful regular expressions for text parsing. &lt;a href="http://windows-powershell-scripts.blogspot.com/2009/06/awk-equivalent-in-windows-powershell.html"&gt;read more...&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;cd - in Powershell&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;One of the cool features of linux bash is the ability to go to the previous working directory. In Bash, we can go to the previous working directory using "cd -" command. &lt;br /&gt;&lt;br /&gt;Unfortunately, we do not have an equivalent of "cd -" in Powershell. So I just wrote a simple powershell function for this cool feature. &lt;a href="http://windows-powershell-scripts.blogspot.com/2009/07/cd-change-to-previous-working-directory.html"&gt;read more...&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;find command in Powershell&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Find is one of the most often used commands in day to day work life. Unix find command provides lot of features and options that enable users to find files/directories more effectively&lt;br /&gt;&lt;br /&gt;In Powershell, we can use Get-ChildItem cmdlet to search for files. But it doesn't give you all the fun. So I have developed Find-ChildItem Powershell cmdlet which is equivalent to Unix find command. &lt;a href="http://windows-powershell-scripts.blogspot.com/2009/08/unix-linux-find-equivalent-in.html"&gt;read more...&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7774240277251981464-6309733130664605882?l=windows-powershell-scripts.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://windows-powershell-scripts.blogspot.com/feeds/6309733130664605882/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://windows-powershell-scripts.blogspot.com/2009/06/unix-equivalents-in-powershell.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7774240277251981464/posts/default/6309733130664605882'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7774240277251981464/posts/default/6309733130664605882'/><link rel='alternate' type='text/html' href='http://windows-powershell-scripts.blogspot.com/2009/06/unix-equivalents-in-powershell.html' title='Unix Equivalents in Powershell'/><author><name>Jagadish</name><uri>http://www.blogger.com/profile/18242041135736190793</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='29' src='http://1.bp.blogspot.com/_cBBGhuN8esk/SebErT0XphI/AAAAAAAAAEE/kNhdrJF4928/S220/DSC00119.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh4.ggpht.com/_cBBGhuN8esk/SjwHE6YsRaI/AAAAAAAAAFw/zI2jI-lw_O0/s72-c/8.gif' height='72' width='72'/><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7774240277251981464.post-703208533717040604</id><published>2009-06-13T17:21:00.001+05:30</published><updated>2009-08-11T20:22:26.156+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Regex'/><title type='text'>Regular Expressions in Powershell</title><content type='html'>Regular Expressions are the most powerful pattern matching techniques available for text parsing. Almost all programming languages have regular expressions. Perl has very powerful regex engine and it provides more features.&lt;br /&gt;&lt;br /&gt;Windows Powershell carries excellent regular expression support from .Net Framework&lt;br /&gt;&lt;br /&gt;Regex Pattern matching in Powershell is so easy. We will go though some examples on Regular Expressions in Windows Powershell&lt;br /&gt;&lt;br /&gt;Prerequisite: Some knowledge on regular expression would be good&lt;br /&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS C:\Scripts&gt; 'Hello World' -match '^H'&lt;br /&gt;True&lt;br /&gt;PS C:\Scripts&gt; 'ello World' -match '^H'&lt;br /&gt;False&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;Powershell returns &lt;b&gt;True&lt;/b&gt; if the match is found, otherwise &lt;b&gt;False&lt;/b&gt; is returned&lt;br /&gt;&lt;br /&gt;Let's see one more example&lt;br /&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS C:\Scripts&gt; '2009-Jun-13' -match '\d{4}-[A-z]{3}-\d{2}'&lt;br /&gt;True&lt;br /&gt;PS C:\Scripts&gt; '2009-June-13' -match '\d{4}-[A-z]{3}-\d{2}'&lt;br /&gt;False&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;code&gt;&lt;font size=3&gt;-replace&lt;/font&gt;&lt;/code&gt; operator is used for Search and Replace in Powershell&lt;br /&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS C:\Scripts&gt; '2009-June-13' -replace "2009", "2010"&lt;br /&gt;2010-June-13&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;How do you capture the pattern and use it in Search and Replace in Powershell? Lets see how,&lt;br /&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS C:\Scripts&gt; '2009-Jun-13' -replace "(\d{4})-([A-z]{3})-(\d{2})", "$2 $3 $1"&lt;br /&gt;&lt;br /&gt;PS C:\Scripts&gt; '2009-Jun-13' -replace "(\d{4})-([A-z]{3})-(\d{2})", '$2 $3 $1'&lt;br /&gt;Jun 13 2009&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;In Search and Replace, Powershell stores captured patterns in special variables $1, $2, etc. But if you use those special variables inside double-quoted replacements, they will be considered as normal variables. That's the reason, we didn't get expected result in first statement in the above code window. To make it work, you need to use single-quoted replacements like above.&lt;br /&gt;&lt;br /&gt;To get more idea on this, let's see another example&lt;br /&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS C:\Scripts&gt; $1="How"&lt;br /&gt;PS C:\Scripts&gt; $2="are"&lt;br /&gt;PS C:\Scripts&gt; $3="you"&lt;br /&gt;PS C:\Scripts&gt;&lt;br /&gt;PS C:\Scripts&gt; '2009-Jun-13' -replace "(\d{4})-([A-z]{3})-(\d{2})", "$2 $3 $1"&lt;br /&gt;are you How&lt;br /&gt;PS C:\Scripts&gt; '2009-Jun-13' -replace "(\d{4})-([A-z]{3})-(\d{2})", '$2 $3 $1'&lt;br /&gt;Jun 13 2009&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;Now let's see how to use captured patterns with &lt;code&gt;&lt;font size=3&gt;-match&lt;/font&gt;&lt;/code&gt; operator. If you capture patterns using &lt;code&gt;&lt;font size=3&gt;-match&lt;/font&gt;&lt;/code&gt;, unlike Search and Replace, the captured patterns will be stored in a special array variable called $matches.&lt;br /&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS C:\Scripts&gt; '2009-Jun-13' -match '(\d{4}).*'&lt;br /&gt;True&lt;br /&gt;PS C:\Scripts&gt; $matches&lt;br /&gt;&lt;br /&gt;Name                           Value&lt;br /&gt;----                           -----&lt;br /&gt;1                              2009&lt;br /&gt;0                              2009-Jun-13&lt;br /&gt;&lt;br /&gt;PS C:\Scripts&gt; $matches[1]&lt;br /&gt;2009&lt;br /&gt;PS C:\Scripts&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;Regular Expressions in Powershell are &lt;b&gt;case-insensitive&lt;/b&gt; by default. For case-sensitive pattern matching, we can use the following operators&lt;br /&gt;&lt;br /&gt;1. -cmatch&lt;br /&gt;2. -creplace&lt;br /&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS C:\Scripts&gt; 'HELLO' -match '[A-Z]'&lt;br /&gt;True&lt;br /&gt;PS C:\Scripts&gt; 'HELLO' -match '[a-z]'&lt;br /&gt;True&lt;br /&gt;PS C:\Scripts&gt;&lt;br /&gt;PS C:\Scripts&gt; 'HELLO' -cmatch '[A-Z]'&lt;br /&gt;True&lt;br /&gt;PS C:\Scripts&gt; 'HELLO' -cmatch '[a-z]'&lt;br /&gt;False&lt;br /&gt;PS C:\Scripts&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7774240277251981464-703208533717040604?l=windows-powershell-scripts.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://windows-powershell-scripts.blogspot.com/feeds/703208533717040604/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://windows-powershell-scripts.blogspot.com/2009/06/regular-expressions-in-powershell.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7774240277251981464/posts/default/703208533717040604'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7774240277251981464/posts/default/703208533717040604'/><link rel='alternate' type='text/html' href='http://windows-powershell-scripts.blogspot.com/2009/06/regular-expressions-in-powershell.html' title='Regular Expressions in Powershell'/><author><name>Jagadish</name><uri>http://www.blogger.com/profile/18242041135736190793</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='29' src='http://1.bp.blogspot.com/_cBBGhuN8esk/SebErT0XphI/AAAAAAAAAEE/kNhdrJF4928/S220/DSC00119.JPG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7774240277251981464.post-7694872956974971566</id><published>2009-06-06T11:40:00.001+05:30</published><updated>2009-08-11T20:22:40.760+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='AWK Equivalent'/><title type='text'>AWK equivalent in Windows Powershell</title><content type='html'>Powershell has lot of features and abilities for text parsing. AWK is one of very powerful commands available for text parsing in Unix/Linux. We do not have a Awk like cmdlet in Powershell. But we can do everything in Powershell that can be done with Awk.&lt;br /&gt;&lt;br /&gt;Powershell combined with .Net Classes provide very powerful regular expressions for text parsing.&lt;br /&gt;&lt;br /&gt;Now, lets play with Bash Awk and Powershell. I'm going to create a test.csv file for our testing and use that through out this topic.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Content of test.csv for our testing&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;1,Tony Passaquale,7920,20090222 21:59:00,800,4.78,3824,Follow-up&lt;br /&gt;2,Nigel Shan Shanford,30316,20090405 16:34:00,400,9.99,3996,New-Opportunity&lt;br /&gt;3,Selma Cooper,97455,20090405 16:31:00,1000,9.99,9990,Pre-Approach&lt;br /&gt;4,Allen James,95140,20090405 16:31:00,1000,9.99,9990,New-Opportunity&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;So, Lets jump in&lt;br /&gt;&lt;br /&gt;Display second field in test.csv&lt;br /&gt;&lt;br /&gt;&lt;div class="bashdivstyle"&gt;&lt;br /&gt;[jagadish.g@localhost Scripts]$ cat test.csv | awk -F, '{print $2}'&lt;br /&gt;Tony Passaquale&lt;br /&gt;Nigel Shan Shanford&lt;br /&gt;Selma Cooper&lt;br /&gt;Allen James&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS C:\Scripts&gt; Get-Content .\test.csv | %{ $_.Split(',')[1]; }&lt;br /&gt;Tony Passaquale&lt;br /&gt;Nigel Shan Shanford&lt;br /&gt;Selma Cooper&lt;br /&gt;Allen James&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;Now, lets try getting the total value of third field in all the records in test.csv&lt;br /&gt;&lt;br /&gt;&lt;div class="bashdivstyle"&gt;&lt;br /&gt;[jagadish.g@localhost Scripts]$  cat test.csv | awk -F, '{total+=$3} END {print "Total: "total}'&lt;br /&gt;Total: 230831&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS C:\Scripts&gt; Get-Content .\test.csv | %{ [int]$total+=$_.Split(',')[2]; } ; Write-Host "Total: $total"&lt;br /&gt;Total: 230831&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;Get no of fields in each record&lt;br /&gt;&lt;br /&gt;&lt;div class="bashdivstyle"&gt;&lt;br /&gt;[jagadish.g@localhost Scripts]$ cat test.csv | awk -F, '{print "No of fields in record "$1" = "NF }'&lt;br /&gt;No of fields in record 1 = 8&lt;br /&gt;No of fields in record 2 = 8&lt;br /&gt;No of fields in record 3 = 8&lt;br /&gt;No of fields in record 4 = 8&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS C:\Scripts&gt; Get-Content .\test.csv | %{ $a=$_.Split(','); Write-Host "No of fields in record"$a[0]"="$a.length; }&lt;br /&gt;No of fields in record 1 = 8&lt;br /&gt;No of fields in record 2 = 8&lt;br /&gt;No of fields in record 3 = 8&lt;br /&gt;No of fields in record 4 = 8&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;Regular Expression matching in Awk and Powershell. Print a record if the last field contains any of these lowercase characters (a, b or c)&lt;br /&gt;&lt;br /&gt;&lt;div class="bashdivstyle"&gt;&lt;br /&gt;[jagadish.g@localhost Scripts]$ cat test.csv | awk -F, '{if ($NF ~ "[a-c]") print}'&lt;br /&gt;3,Selma Cooper,97455,20090405 16:31:00,1000,9.99,9990,Pre-Approach&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS C:\Scripts&gt; Get-Content .\test.csv | %{ if ($_.Split(',')[-1] -match "[a-c]") { $_; } }&lt;br /&gt;3,Selma Cooper,97455,20090405 16:31:00,1000,9.99,9990,Pre-Approach&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7774240277251981464-7694872956974971566?l=windows-powershell-scripts.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://windows-powershell-scripts.blogspot.com/feeds/7694872956974971566/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://windows-powershell-scripts.blogspot.com/2009/06/awk-equivalent-in-windows-powershell.html#comment-form' title='14 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7774240277251981464/posts/default/7694872956974971566'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7774240277251981464/posts/default/7694872956974971566'/><link rel='alternate' type='text/html' href='http://windows-powershell-scripts.blogspot.com/2009/06/awk-equivalent-in-windows-powershell.html' title='AWK equivalent in Windows Powershell'/><author><name>Jagadish</name><uri>http://www.blogger.com/profile/18242041135736190793</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='29' src='http://1.bp.blogspot.com/_cBBGhuN8esk/SebErT0XphI/AAAAAAAAAEE/kNhdrJF4928/S220/DSC00119.JPG'/></author><thr:total>14</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7774240277251981464.post-7977291694229141381</id><published>2009-06-01T12:20:00.001+05:30</published><updated>2009-08-11T20:22:55.567+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Registry'/><title type='text'>Working with Registry in Powershell</title><content type='html'>Microsoft has made registry accessing very simple using powershell. You can access the system registry just like any other drive in powershell. &lt;br /&gt;&lt;br /&gt;For example, you can easily list all the registry entries in &lt;b&gt;HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run&lt;/b&gt; registry key with Get-ItemProperty cmdlet&lt;br /&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS C:\&gt; cd HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run&lt;br /&gt;PS HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run&gt; Get-ItemProperty .&lt;br /&gt;&lt;br /&gt;RTHDCPL              : RTHDCPL.EXE&lt;br /&gt;Alcmtr               : ALCMTR.EXE&lt;br /&gt;IgfxTray             : C:\WINDOWS\system32\igfxtray.exe&lt;br /&gt;HotKeysCmds          : C:\WINDOWS\system32\hkcmd.exe&lt;br /&gt;Persistence          : C:\WINDOWS\system32\igfxpers.exe&lt;br /&gt;&lt;br /&gt;PS HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;You can add a new entry to registry key using New-ItemProperty cmdlet&lt;br /&gt;&lt;br /&gt;I have just added a entry to &lt;b&gt;HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run&lt;/b&gt; to autostart notepad.exe on the windows boot-up&lt;br /&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS C:\&gt; New-ItemProperty -path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run -name "Notepad" -value "C:\WINDOWS\NOTEPAD.EXE" -type string&lt;br /&gt;&lt;br /&gt;Notepad&lt;br /&gt;-------&lt;br /&gt;C:\WINDOWS\NOTEPAD.EXE&lt;br /&gt;&lt;br /&gt;PS C:\&gt; Get-ItemProperty -path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run&lt;br /&gt;&lt;br /&gt;RTHDCPL              : RTHDCPL.EXE&lt;br /&gt;Alcmtr               : ALCMTR.EXE&lt;br /&gt;IgfxTray             : C:\WINDOWS\system32\igfxtray.exe&lt;br /&gt;HotKeysCmds          : C:\WINDOWS\system32\hkcmd.exe&lt;br /&gt;Persistence          : C:\WINDOWS\system32\igfxpers.exe&lt;br /&gt;Notepad              : C:\WINDOWS\NOTEPAD.EXE&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;As you might have noticed in the above code window, i have used PropertyType as 'String'. Below is the list of PropertyType Values available for Registry Entries&lt;br /&gt;&lt;br /&gt;&lt;table class="tablestyle"&gt;&lt;tr class="tablehed"&gt;&lt;td&gt;PropertyType Value&lt;/td&gt;&lt;td&gt;Meaning&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Binary&lt;/td&gt;&lt;td&gt;Binary data&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;DWord&lt;/td&gt;&lt;td&gt;A number that is a valid UInt32&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;ExpandString&lt;/td&gt;&lt;td&gt;A string that can contain environment variables that are dynamically expanded&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;MultiString&lt;/td&gt;&lt;td&gt;A multiline string&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;String&lt;/td&gt;&lt;td&gt;Any string value&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;QWord&lt;/td&gt;&lt;td&gt;8 bytes of binary data&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;A registry entry can be removed from a registry key with the help of Remove-ItemProperty cmdlet&lt;br /&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS C:\&gt; Remove-ItemProperty -path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run -name "Notepad"&lt;br /&gt;PS C:\&gt; Get-ItemProperty -path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run&lt;br /&gt;&lt;br /&gt;RTHDCPL              : RTHDCPL.EXE&lt;br /&gt;Alcmtr               : ALCMTR.EXE&lt;br /&gt;IgfxTray             : C:\WINDOWS\system32\igfxtray.exe&lt;br /&gt;HotKeysCmds          : C:\WINDOWS\system32\hkcmd.exe&lt;br /&gt;Persistence          : C:\WINDOWS\system32\igfxpers.exe&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7774240277251981464-7977291694229141381?l=windows-powershell-scripts.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://windows-powershell-scripts.blogspot.com/feeds/7977291694229141381/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://windows-powershell-scripts.blogspot.com/2009/05/working-with-registry-in-powershell.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7774240277251981464/posts/default/7977291694229141381'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7774240277251981464/posts/default/7977291694229141381'/><link rel='alternate' type='text/html' href='http://windows-powershell-scripts.blogspot.com/2009/05/working-with-registry-in-powershell.html' title='Working with Registry in Powershell'/><author><name>Jagadish</name><uri>http://www.blogger.com/profile/18242041135736190793</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='29' src='http://1.bp.blogspot.com/_cBBGhuN8esk/SebErT0XphI/AAAAAAAAAEE/kNhdrJF4928/S220/DSC00119.JPG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7774240277251981464.post-6098995043436240355</id><published>2009-05-03T10:43:00.001+05:30</published><updated>2009-08-11T20:23:26.387+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Set-ExecutionPolicy'/><title type='text'>How to run windows powershell scripts</title><content type='html'>&lt;font color="#012456"&gt;R&lt;/font&gt;unning Powershell Scripts on local machine is so easy. You just need to change ExecutionPolicy in the Powershell. Powershell Execution Policies are used to prevent unauthorized scripts from running. They provide security for the scripting environment.&lt;br /&gt;&lt;br /&gt;In Unix-like machines, we need to set the executable bit for shell scripts to run them (chmod +x &amp;lt;scriptname&amp;gt;)&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Windows Powershell Execution Policies&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;       Restricted&lt;br /&gt;           - Default execution policy.&lt;br /&gt;           - Permits individual commands, but scripts cannot&lt;br /&gt;             run.&lt;br /&gt;&lt;br /&gt;       AllSigned&lt;br /&gt;           - Scripts can run.&lt;br /&gt;           - Requires a digital signature from a trusted&lt;br /&gt;             publisher on all scripts and configuration files,&lt;br /&gt;             including scripts that you write on the local&lt;br /&gt;             computer.&lt;br /&gt;           - Prompts you before running scripts from trusted&lt;br /&gt;             publishers.&lt;br /&gt;           - Risks running signed, but malicious, scripts.&lt;br /&gt;&lt;br /&gt;       RemoteSigned&lt;br /&gt;           - Scripts can run.&lt;br /&gt;           - Requires a digital signature from a trusted&lt;br /&gt;             publisher on scripts and configuration files that&lt;br /&gt;             are downloaded from the Internet (including&lt;br /&gt;             e-mail and instant messaging programs).&lt;br /&gt;           - Does not require digital signatures on scripts run&lt;br /&gt;             from the local computer.&lt;br /&gt;           - Does not prompt you before running scripts from&lt;br /&gt;             trusted publishers.&lt;br /&gt;           - Risks running signed, but malicious, scripts.&lt;br /&gt;&lt;br /&gt;       Unrestricted&lt;br /&gt;           - Unsigned scripts can run.&lt;br /&gt;           - Scripts and configuration files that are downloaded&lt;br /&gt;             from the Internet (including Microsoft Outlook,&lt;br /&gt;             Outlook Express and Windows Messenger) run after&lt;br /&gt;             warning you that the file originated on the Internet.&lt;br /&gt;           - Risks running malicious scripts.&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;For us to learn, we will use the ExecutionPolicy "RemoteSigned" to run Powershell scripts without digital signatures on the local machine.&lt;br /&gt;&lt;br /&gt;I have written a script to display first 5 running services. Let's see how to run that script (List_5_Running_Services.ps1) &lt;br /&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS C:\Users\Jagadish\Documents\Scripts&gt; Get-ExecutionPolicy&lt;br /&gt;Restricted&lt;br /&gt;&lt;br /&gt;PS C:\Users\Jagadish\Documents\Scripts&gt; Get-Content List_5_Running_Services.ps1&lt;br /&gt;Get-Service | Where-Object { $_.Status -eq "Running" } | Select-Object -First 5&lt;br /&gt;&lt;br /&gt;PS C:\Users\Jagadish\Documents\Scripts&gt; .\List_5_Running_Services.ps1&lt;br /&gt;&lt;span class="codeerror"&gt;File C:\Users\Jagadish\Documents\Scripts\List_5_Running_Services.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details.&lt;br /&gt;At line:1 char:14&lt;br /&gt;+ .\Testing.ps1 &lt;&lt;&lt;&lt;&lt;br /&gt;    + CategoryInfo          : NotSpecified: (:) [], PSSecurityException&lt;br /&gt;    + FullyQualifiedErrorId : RuntimeException&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;PS C:\Users\Jagadish\Documents\Scripts&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;As you can see in the above code, the current ExecutionPolicy is "Restricted". "Restricted" ExecutionPolicy is the most secure policy, and is the default. It permits individual commands, but does not permit scripts to run.&lt;br /&gt;&lt;br /&gt;So to run our scripts without digital signatures, we need to change it to "RemoteSigned" Policy.&lt;br /&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS C:\Users\Jagadish\Documents\Scripts&gt; Get-ExecutionPolicy&lt;br /&gt;Restricted&lt;br /&gt;PS C:\Users\Jagadish\Documents\Scripts&gt; Set-ExecutionPolicy RemoteSigned&lt;br /&gt;&lt;span class="codeerror"&gt;Set-ExecutionPolicy : Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied.&lt;br /&gt;At line:1 char:20&lt;br /&gt;+ Set-ExecutionPolicy &lt;&lt;&lt;&lt;  RemoteSigned&lt;br /&gt;    + CategoryInfo          : NotSpecified: (:) [Set-ExecutionPolicy], UnauthorizedAccessException&lt;br /&gt;    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;PS C:\Users\Jagadish\Documents\Scripts&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;If you get the above error while changing ExecutionPolicy to RemoteSigned, then probably you are running the Windows Powershell without Administrator Privileges. You can easily start Windows Powershell with Administrator Privilege just by right-clicking on the Powershell icon and selecting "Run as Administrator" as shown in below picture&lt;br /&gt;&lt;br /&gt;Users running Windows XP or Windows Server 2003, may not get the above error as most users login with administrative privileges. In Windows Vista/7, this changed with the advent of User Account Control. Unless explictly requested, processes started by an Administrator account will run as a standard user and not Administrator.&lt;br /&gt;&lt;br /&gt;&lt;img src="http://lh6.ggpht.com/_cBBGhuN8esk/Sf1SULw45tI/AAAAAAAAAFE/70n63eNPukk/s400/powershell_with_admin.png" /&gt;&lt;br /&gt;&lt;br /&gt;After starting Windows Powershell with Administrator Privileges, lets try changing the ExecutionPolicy and running the script&lt;br /&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS C:\Users\Jagadish\Documents\Scripts&gt; Get-ExecutionPolicy&lt;br /&gt;Restricted&lt;br /&gt;PS C:\Users\Jagadish\Documents\Scripts&gt; Set-ExecutionPolicy RemoteSigned&lt;br /&gt;PS C:\Users\Jagadish\Documents\Scripts&gt; Get-ExecutionPolicy&lt;br /&gt;RemoteSigned&lt;br /&gt;&lt;br /&gt;PS C:\Users\Jagadish\Documents\Scripts&gt; .\List_5_Running_Services.ps1&lt;br /&gt;&lt;br /&gt;Status   Name               DisplayName&lt;br /&gt;------   ----               -----------&lt;br /&gt;Running  AntiVirSchedule... Avira AntiVir Scheduler&lt;br /&gt;Running  AntiVirService     Avira AntiVir Guard&lt;br /&gt;Running  Appinfo            Application Information&lt;br /&gt;Running  AudioEndpointBu... Windows Audio Endpoint Builder&lt;br /&gt;Running  Audiosrv           Windows Audio&lt;br /&gt;&lt;br /&gt;PS C:\Users\Jagadish\Documents\Scripts&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;More details can be found in &lt;a href="http://www.microsoft.com/technet/scriptcenter/topics/winpsh/manual/run.mspx"&gt;http://www.microsoft.com/technet/scriptcenter/topics/winpsh/manual/run.mspx&lt;/a&gt;&lt;br&gt;&lt;br /&gt;Let me know if you have any questions or comments please&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7774240277251981464-6098995043436240355?l=windows-powershell-scripts.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://windows-powershell-scripts.blogspot.com/feeds/6098995043436240355/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://windows-powershell-scripts.blogspot.com/2009/05/how-to-run-windows-powershell-scripts.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7774240277251981464/posts/default/6098995043436240355'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7774240277251981464/posts/default/6098995043436240355'/><link rel='alternate' type='text/html' href='http://windows-powershell-scripts.blogspot.com/2009/05/how-to-run-windows-powershell-scripts.html' title='How to run windows powershell scripts'/><author><name>Jagadish</name><uri>http://www.blogger.com/profile/18242041135736190793</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='29' src='http://1.bp.blogspot.com/_cBBGhuN8esk/SebErT0XphI/AAAAAAAAAEE/kNhdrJF4928/S220/DSC00119.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh6.ggpht.com/_cBBGhuN8esk/Sf1SULw45tI/AAAAAAAAAFE/70n63eNPukk/s72-c/powershell_with_admin.png' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7774240277251981464.post-3179937217912349965</id><published>2009-04-16T12:55:00.001+05:30</published><updated>2009-05-05T13:00:56.056+05:30</updated><title type='text'>First Powershell Script</title><content type='html'>&lt;span style=";font-family:verdana;font-size:100%;"  &gt;&lt;a href="http://en.wikipedia.org/wiki/Windows_Powershell" /&gt;Windows Powershell&lt;/a&gt; is another attempt from Redmond to provide powerful windows scripting language to IT professionals. But this time, I would say that it is a successful attempt by Microsoft.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=";font-family:verdana;font-size:100%;"  &gt;Windows Powershell is often being compared with Unix Bash Shell. But it actually does more than what you may expect. Unlike its Unix counterpart, the PowerShell pipeline is an object pipeline; that is, the data passed between cmdlets are fully typed objects, rather than byte streams. A cmdlet, pronounced "command-let" is same as what you call "shell script" in Unix. So don’t get confused with cmdlets&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=";font-family:verdana;font-size:100%;"  &gt;I have read more reviews on Powershell and excited to see Powershell in action. I'm posting all my learning as I learn so it could be useful to everyone.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=60deac2b-975b-41e6-9fa0-c2fd6aa6bc89&amp;displaylang=en"&gt;Click here&lt;/a&gt; to download Windows PowerShell 2.0 Community Technology Preview (CTP)&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:verdana; font-size:100%;"&gt;So Here is your first simple “Hello World” script:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS C:\Documents and Settings\jagadish.g&gt; Write-Host "Hello World"&lt;br /&gt;Hello World&lt;br /&gt;PS C:\Documents and Settings\jagadish.g&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;Write-Host cmdlet is similar to echo in Unix. You can specify separator, background color, foreground color and more.&lt;br /&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS C:\Documents and Settings\jagadish.g&gt; Write-Host (10,10,12)&lt;br /&gt;10 10 12&lt;br /&gt;PS C:\Documents and Settings\jagadish.g&gt; Write-Host (10,10,12) -Separator "+"&lt;br /&gt;10+10+12&lt;br /&gt;PS C:\Documents and Settings\jagadish.g&gt; Write-Host (10,10,12) -Separator "+" -ForegroundColor Green -BackgroundColor Black&lt;br /&gt;&lt;span style="background-color: Black; color: LightGreen;"&gt;10+10+12&lt;/span&gt;&lt;br /&gt;PS C:\Documents and Settings\jagadish.g&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Now we will go through some simple cmdlets.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Get-Process&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Gets the processes that are running on the local computer or a remote computer&lt;br /&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS C:\Documents and Settings\jagadish.g&gt; Get-Process | Select-Object -First 3&lt;br /&gt;&lt;br /&gt;Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName&lt;br /&gt;-------  ------    -----      ----- -----   ------     -- -----------&lt;br /&gt;     32       2      780       2708    28     0.05   1296 acrotray&lt;br /&gt;    107       5     1168       3524    32     0.03   2712 alg&lt;br /&gt;    203       7     3584       6648    43     0.36   3724 btdna&lt;br /&gt;&lt;br /&gt;PS C:\Documents and Settings\jagadish.g&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;Get-Service&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Gets the services on a local or remote computer&lt;br /&gt;&lt;br /&gt;&lt;div class="codedivstyle"&gt;&lt;br /&gt;PS C:\Documents and Settings\jagadish.g&gt; Get-Service | Select-Object -First 3&lt;br /&gt;&lt;br /&gt;Status   Name               DisplayName&lt;br /&gt;------   ----               -----------&lt;br /&gt;Stopped  Alerter            Alerter&lt;br /&gt;Running  ALG                Application Layer Gateway Service&lt;br /&gt;Stopped  AppMgmt            Application Management&lt;br /&gt;&lt;br /&gt;PS C:\Documents and Settings\jagadish.g&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;If you have any questions on Powershell, please post them in the below comments section&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7774240277251981464-3179937217912349965?l=windows-powershell-scripts.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://windows-powershell-scripts.blogspot.com/feeds/3179937217912349965/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://windows-powershell-scripts.blogspot.com/2009/04/windows-powershell-is-another-attempt.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7774240277251981464/posts/default/3179937217912349965'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7774240277251981464/posts/default/3179937217912349965'/><link rel='alternate' type='text/html' href='http://windows-powershell-scripts.blogspot.com/2009/04/windows-powershell-is-another-attempt.html' title='First Powershell Script'/><author><name>Jagadish</name><uri>http://www.blogger.com/profile/18242041135736190793</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='29' src='http://1.bp.blogspot.com/_cBBGhuN8esk/SebErT0XphI/AAAAAAAAAEE/kNhdrJF4928/S220/DSC00119.JPG'/></author><thr:total>3</thr:total></entry></feed>
