11
Aug
stored in: Powershell and tagged:
UNIX users who are so used to grep would ask “how do I grep in Powershell?”. Here’s how:
In Bash we write:
ls | grep ".aspx"
The equivalent in Powershell would be:
ls | where {$_ -match ".aspx"}
Bash@Linux output:
[root@gw-1 easilogin]# ls -all | grep ".aspx" -rwxrwSrwt 1 root root 682 Jul 29 20:38 AccountBanned.aspx -rwxrwSrwt 1 root root 77 Jul 29 20:38 AccountBanned.aspx.vb -rwxrwSrwt 1 root root 2320 Jul 29 20:38 CodeAuthentication.aspx -rwxrwSrwt 1 root root 1623 Jul 29 20:38 CodeAuthentication.aspx.vb -rwxrwSrwt 1 root root 1459 Aug 1 14:25 ForgetPassword.aspx -rwxrwSrwt 1 root root 3563 Aug 1 14:25 ForgetPassword.aspx.vb -rwxrwSrwt 1 root root 2888 Jul 29 20:38 login.aspx -rwxrwSrwt 1 root root 8298 Jul 29 20:38 login.aspx.vb -rwxrwSrwt 1 root root 98 Jun 23 12:54 logout.aspx -rwxrwSrwt 1 root root 2864 Aug 3 21:12 My_Account.aspx -rwxrwSrwt 1 root root 1704 Aug 3 21:12 My_Account.aspx.vb -rwxrwSrwt 1 root root 475 Jul 29 20:38 NotAuthenticated.aspx -rwxrwSrwt 1 root root 78 Jul 29 20:38 NotAuthenticated.aspx.vb
Powershell@Windows2008 output:
PS Microsoft.PowerShell.Core\FileSystem::\\192.168.1.11\inetpub\wwwroot\easilogin> ls | where {$_ -match ".aspx"}
Directory: Microsoft.PowerShell.Core\FileSystem::\\192.168.1.11\inetpub\wwwroot\easilogin
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 7/29/2008 8:38 PM 682 AccountBanned.aspx
-a--- 7/29/2008 8:38 PM 77 AccountBanned.aspx.vb
-a--- 7/29/2008 8:38 PM 2320 CodeAuthentication.aspx
-a--- 7/29/2008 8:38 PM 1623 CodeAuthentication.aspx.vb
-a--- 8/1/2008 2:25 PM 1459 ForgetPassword.aspx
-a--- 8/1/2008 2:25 PM 3563 ForgetPassword.aspx.vb
-a--- 7/29/2008 8:38 PM 2888 login.aspx
-a--- 7/29/2008 8:38 PM 8298 login.aspx.vb
-a--- 6/23/2008 12:54 PM 98 logout.aspx
-a--- 8/3/2008 9:12 PM 2864 My_Account.aspx
-a--- 8/3/2008 9:12 PM 1704 My_Account.aspx.vb
-a--- 7/29/2008 8:38 PM 475 NotAuthenticated.aspx
-a--- 7/29/2008 8:38 PM 78 NotAuthenticated.aspx.vb
June 29th, 2010 at 4:15 pm
Thanks for this post. I was on the wrong track till I found this. I just happen to be wanting to filter the Process list too!
September 29th, 2010 at 5:29 pm
One caveat to this post…if you want to grep on a particular field other than the last one in the list, you have to explicitly specify the object. Using the example above, to grep on the file length equaling 2888, you would have to use:
ls | where {$_.Length -match 2888}
October 25th, 2010 at 4:17 pm
Thanks for the tip Nelson!
December 24th, 2011 at 1:22 am
grep reads the content of the file, anything close to that in powershell would be get-content. So if you were looking for the string “Successfully Completed” in the .log files of a directory, here’s what you could do:
Get-Content *.log | where {$_ -match “Successfully Completed”}
July 27th, 2012 at 8:36 pm
Nobody writes – ls | grep “.aspx” in bash
If you REALLY want to do this, you write
ls “*?aspx”
because grep sees a regex, so your “.aspx”means: any character followed by aspx”". In your results you might find: afile.kaspx
This probably isn’t what you wanted to do
You meant
ls | grep “\.aspx”
which is the same as
ls “.aspx”
and the last thing is much easier. Use the right tool for the job !
July 27th, 2012 at 8:38 pm
hmm the last command should be
ls *.aspx
of course
October 17th, 2012 at 3:03 am
http://blogs.msdn.com/b/powershell/archive/2008/03/23/select-string-and-grep.aspx
December 19th, 2012 at 10:13 pm
$ new-alias grep findstr
$ dir | grep -i tesT
-a— 10/10/2011 11:55 AM 1761 test-port.ps1
-a— 10/11/2011 10:15 AM 3133 test-ports.ps1
-a— 10/13/2011 3:00 PM 496 test_slurp.ps1