February 27, 2010

Powershell replaced my favorite DOS command

Filed under: Uncategorized — Kevmar @ 11:52 am

My favorite dos shell command is the for loop.  I have used that so many time to automate things for me.

C:\> for /f %i in ('dir /b') do echo %i

I would either pipe that into a file and use it in another for loop, or replace the echo with a command.  ’dir /b’ is the most common command for me to loop on.  That lists all the files in the location but only gives the file name.  I recently discovered powershell and it has this built in with more power.

PS> dir | %{echo $_.name}

Here is the full command without the shorthand.

PS> get-childitem | foreach-object{echo $_.name}

With powershell its much easier to chain things together.  In my CMD shell I would do a for loop and dump it to a file, then i would for loop on that file to a new file.  I would repeat that to get the data the way I want for my final loop command.

Lets say all of my home folders for my users are grouped in department folders and I want to list user folders where the user is not the owner.  I’m not even sure how I would do that off the command prompt but here is how I would do it with powershell.

PS> get-childitem . | foreach-object{$_.getdirectories()} | where-object {(get-acl $_.fullname).owner -ne "domain\" + $_.name} | format-table fullname

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress