Short posts about Microsoft PowerShell as I learn, trying to focus on one aspect at a time.
Showing posts with label Get-ChildItem. Show all posts
Showing posts with label Get-ChildItem. Show all posts
Tuesday, June 30, 2009
Recursively Getting All Folder Sizes
This started out as my entry for Event 8 in the Microsoft Scripting Games 2009 (see more entries here), but it's useful, so I think I'll keep it. This function uses recursion to work its way down the directory tree and get all folders and their sizes, and outputs them as psobjects for easy sorting, etc. It's not the fastest thing in the world, but it does the trick. Make sure to check out the examples in the help documentation.
Labels:
Get-ChildItem,
Get-DirSize,
PowerShell v2,
Recursion
Tuesday, May 19, 2009
Get-ChildItemRecurse Update
Someone named CrazyDave made an important update to the script I made yesterday. I was checking for the specific type System.IO.DirectoryInfo. The problem with this is, it unnecessarily limits my script to being used on files and folders. Get-ChildItem makes no such distinction. It can be used on any PSDrive.
So here is the corrected version: http://poshcode.org/1115 (highlighted line is the old one)
Labels:
Get-ChildItem,
Get-ChildItemRecurse,
Script Updates,
Scripts
Monday, May 18, 2009
A Get-ChildItem Wrapper That Lets You Say How Deep to Recurse
I noticed that a couple of people hit my blog that seemed to be looking for a script like this, and it turned out to be an interesting exercise.
Notice that I took the fileglob and moved it down the script rather than just using it in my call to Get-ChildItem. I had to do that because otherwise I would only have been recursing through directories that matched the pattern, and I wanted to recurse down into all directories, but only send those entries that match the pattern down the pipeline.
The function is here, for those that don't see it inline in the post: http://poshcode.org/1113
Labels:
Get-ChildItem,
Get-ChildItemRecurse,
Recursion,
Scripts
Wednesday, February 18, 2009
Get-ChildItem (dir) Results Color-Coded by Type
I always liked the way you can color code your directory items in Linux. I always set alias ls='ls -al --color' the minute I log into a system. It makes it easier to scan results and instantly know some extra data about the items in a directory.
I decided that I wanted to be able to do this in PowerShell. If you do a few Google searches you'll see I'm not the first, and it's not without it's quirks, but I decided not to worry about being true to the Linux form and just go with the spirit of what I wanted to get out of it. I've included the code below. There are a couple of things of note, though it's mostly waht I wanted:
- I would rather if the header at the top wasn't always the same color as the first item, but I can't think of any way around that.
- Rather than coloring results by permissions, I've colored them by type, with four major types: Directory, Executable, Compressed, and Text.
- If you use sort or select you'll lose the color, but otherwise the color change should last down the pipeline for each obect.
- This was made to be used with v2.0 CTP3 of PowerShell. If you want to use it with earlier versions you have to remove the documentation block at the beginning of the function, but you also lose the ability to use the help command to find out more about the function.
- Note that I compile the Regular Expressions at the beginning. If I just used -match to check the regular expressions for each file, I would add a lot of extra overhead, because the regular expressions would be compiled each time a file was checked. This boosts performance, and there is an analogue in each language that supports regular expressions.
Labels:
Dir,
Get-ChildItem,
Get-ChildItemColor,
Ls
Tuesday, August 12, 2008
Recursion in PowerShell
Recursion is one of those things that you'll probably find that you don't need to use all that often, but it's a really powerful concept, and where it's useful it can save you a lot of time, and not just in PowerShell. Simply put, a recursive function has the ability to call itself, usually to perform a task with an unknown depth.
I remember the first time I hit this problem, and I had just started scripting. I wanted to see who had permissions to a folder. I walked the ACL on the folder and found a group, so wrote a function to grab the members of the group. That's when I realized that some of the group's members were groups. My first solution was horribly unwieldy. I wrote a bunch of if/else statements that were able to grab group memberships up to three levels deep, and as I was looking unhappily at my hard-to-follow code, a spark of a memory started nagging at me, and I remembered an email thread about recursion, and that's the first time I really "got it".
Let's cut to the chase with an easy example: walking a directory tree. The function below will use Get-ChildItem (dir) to grab all child items in the current PSPath and print their FullName. In order to get all files in all child directories, every time it hits a directory it will call itself on the directory.
# Recurse($path, $fileglob)
#
# Recurses through a psdrive and prints all items that match.
#
# Args:
# [string]$path: The starting path
# [string]$fileglob(optional): The search string for matching files
#
function Recurse ([string]$path, [string]$fileglob){
if (-not (Test-Path $path)) {
Write-Error "$path is an invalid path."
return $false
}
$files = @(dir -Path $path -Include $fileglob)
foreach ($file in $files) {
if ($file.GetType().FullName -eq 'System.IO.FileInfo') {
Write-Output $file.FullName
}elseif ($file.GetType().FullName -eq 'System.IO.DirectoryInfo') {
Recurse $file.FullName
}
}
}
In my mind I always picture a cat's cradle-style string looping through itself over and over until the final loop is met and a final tug unravels the whole thing back into a string.
Subscribe to:
Posts (Atom)