Microsoft

Blog Categories

Subscribe to RSS feed

Archives

An Introduction to PowerShell Scripting

So, you’re interested in PowerShell, but aren’t sure if you want to jump right in to writing your own cmdlets? No problem; there are plenty of built-in ones to get us started. Let’s take a look at a few different ways to use cmdlets: calling them directly from PowerShell, stringing multiple cmdlets together using the pipeline, and writing a simple script to automate these tasks.

Say you want to rename a file. Just open PowerShell and use the Rename-Item cmdlet. It takes two parameters: the path to the input file and the new name. You can explicitly specify the names of these parameters, pass them in in the order expected by PowerShell, or enter rename-item and wait for PowerShell to request them.

powershell1

I know what you’re thinking: this is unnecessarily complicated. I agree, but what if we use PowerShell to rename all the files in a folder? The Get-ChildItem cmdlet will give us a list of all the items in a directory, but it also returns these items as objects that we can manipulate.

At the most basic level, this allows you to do things like output the contents of a directory to a text file or sort them by size. On the left side of the pipe, we have a cmdlet that produces a set of objects; on the right side, a cmdlet that operates on those objects.

powershell2

We’re still not doing anything super useful at this point though, but if we pull some of this functionality into a script, we can string together several commands to create a reusable piece of functionality. Let’s return to renaming files. Whether you’re appending a descriptive title to photos or the name of a project you’re working on, at some point, you’ve probably had to rename all the files in a directory. With a few lines of a PowerShell script, we can easily automate this task, saving you precious minutes that you can now use to learn about other cmdlets.

First, you’ll need to create a .ps1 file; any text editor will work, but you’ll want to check out PowerGUI if you get serious about scripting.

param($path, $prefix)

Get-ChildItem -Path $path |
Foreach-Object -Process
{
    $newName = [System.String]::Format("{0}{1}", $prefix, $_.name)
    Rename-Item -Path $_.FullName -NewName $newName
}

We’re going to pass in two parameters: path (the directory containing the files to be renamed) and prefix (the string to be added to the filename). From there, it’s just a matter of stringing together the Get-ChildItem, ForEach-Object, and Rename-Item cmdlets. To run the script, simply navigate to the directory and use “.” plus the script name.

rename1

After running the script, you can use Get-ChildItem again to verify that the prefix has been added to the filenames. With a minimal amount of effort, you could modify this script to add suffixes, rename files in subdirectories, or change extensions. Once you learn how to write your own cmdlets, the possibilities will really be endless.

Leave a Reply