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

When you use the get-wmiobject cmdlet to retrieve properties of a Windows Management Instrumentation, WMI, class you can be overwhelmed by the number of properties displayed. Among the properties displayed when you use an asterisk wildcard with the format-list cmdlet,

get-wmiobject -Class Win32_BIOS |

format-list *

are the WMI system properties which you probably don't want to see. You can use a more specific wildcard with the format-list cmdlet to filter the properties displayed.

If you want to display properties about the BIOS of your machine you might use the command but exclude the WMI system properties use this command:

get-wmiobject -Class Win32_BIOS |

format-list [a-z]*

How does this work? The first step of the pipeline uses the Win32_BIOS WMI class to pass its properties to the second step of the pipeline. The format-list cmdlet formats the output as a list, but which properties are to be displayed?

The [a-z]* is an extended wildcard. It's partly a traditional wildcard and partly like a regular expression. The [a-z] is a character class. It matches any lower case alphabetic character between a and z. The * matches zero or more characters. Taken together the wildcard means "Match any sequence of characters that begins with an alphabetic character".

You can use more focussed wilcards. To retrieve the properties beginning with s use this command:

get-wmiobject -Class Win32_BIOS |

format-list s*

The pattern s* matches any sequence of characters where the first character is s.
Technorati Profile