Limitations of the rename-item cmdlet

May 13, 2006

If you've been using Monad betas before the release of PowerShell RC1 you quite possibly have a significant number of scripts that you wrote using the Monad betas. Those files will have a .msh file extension. When you upgrade to PowerShell, apart from any changes you need to make to the content, you need to change the file extension to .ps1.

In the good old Windows command shell, doing that is straightforward. Simply navigate to the folder holding the scripts. Then use the following command:

ren *.msh *.ps1

If you try that with Monad you receive the following error:

PS C:\PowerShellScripts> ren *.msh *.ps1
Rename-Item : Cannot process argument because the value of argument "path" is invalid. At line:1 char:4
+ ren <<<< *.msh *.ps1

So, what's wrong with the path? To figure that out I will show the command in a full form:

ren -Path *.msh -NewName *.ps1

The error relates to the value of the Path parameter. It accepts wildcards in the path but it must resolve to a single file.

So how do you use PowerShell to rename multiple files?

PowerShell is designed to use pipelines. And you need a two step pipeline to get the job done. Assuming you are in the directory that contains the files that you want to rename, use the following command:

get-childitem -Path *.msh |

rename-item -NewName {$_.name -replace ".msh",".ps1"} -whatif

As you can see it's a pipeline with two steps. The first step uses the get-childitem cmdlet to retrieve information about all files in the current folder with a .msh file extension. The second step uses the rename-item cmdlet to process each object passed on from the first step. Each object is a FileInfo object. The value of the Extension property of each FileInfo object is .msh, as you can see by using the following command:

get-childitem *.msh |

format-table Name, Extension

The value of the NewName parameter is {$_.name -replace ".msh",".ps1"}. Any occurrence of .msh in the value of the Name property of the current object is replaced by .ps1.

To put it more succinctly, all files with a file extension of .msh are renamed to have a file extension of .ps1.

In Windows Explorer you can rename the current folder. The rename-item cmdlet doesn't allow you to rename any folder.

The example I showed used the FileSystem provider. You can use the rename-item cmdlet to rename items in other providers, for example the alias and variable providers. To display available providers use this command:

get-PSDrive

The get-PSDrive cmdlet has replaced the get-drive cmdlet which was found in public builds before RC1.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: