Use PowerShell to display Short File and Folder Names

Doctor Scripto

Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to display short file and folder names.

Hey, Scripting Guy! Question Hey, Scripting Guy! I need to be able to use Windows PowerShell to show short folder and file names. In the old days, I could see short file and folder names in the CMD prompt, but these days when I am using Windows PowerShell I do not find these things. I know they look strange, and I need a systematic way to display the file and folder names without permitting them to stretch on and on. Please help me—this is a hard stop for my current project.

—SH

Hey, Scripting Guy! Answer Hello SH,

Microsoft Scripting Guy, Ed Wilson, is here. WooHoo! Today is Thursday, and the excitement is buzzing around the Scripting House in Charlotte, North Carolina. No, it is not because we are one more day closer to the weekend. Today is a special Thursday. First, we have the Charlotte Windows PowerShell User Group meeting at 6:30 at the Microsoft Office in Charlotte. Then it will be followed with the PowerScripting Podcast. Yepper! It will be all PowerShell, all day—and that is always a good thing.

Note  I want to give credit to Microsoft PFE, Chris Wu, who is responsible for much of what is in today’s post by answering a question on our internal Windows PowerShell alias.

Use the old-fashioned CMD prompt

One of my favorite things to do is to run the old-fashioned CMD interpreter that is inside of Windows PowerShell. I know. It is my weird sense of humor, but it does make my inner geek smile. This works because CMD.EXE is simply another application. And I can run executables inside of Windows PowerShell with no problems. Therefore, I open Windows PowerShell, and run CMD inside it.

This is actually a useful technique at times. I run CMD with the /c parameter. The /c parameter tells CMD to run a command and then exit. The command I want to run is dir /x. Dir, of course, obtains a directory listing, and /x provides for extension information. The command is shown here:

cmd /c dir /x

The command and the associated output from the command are shown in the following image.

Image of command output

Of course, if I want to be able to do anything with the output, I need to capture it, parse it, and convert it to an object. For me, that is too much trouble; especially when a method exists from an unlikely source.

Using the FileSystemObject to create short names

I used the Scripting.FileSystemObject object (yes, there really is an “object object” thing going here) back in the VBScript days. It is an extremely fast and powerful object. To see some of the things I can do with FileSystemObject, I pipe the object to the Get-Member cmdlet:

$fso = New-Object -com scripting.filesystemobject

$fso | gm -mem method

The resulting output from the command is shown here:

Image of command output

When I inspect the methods available from FileSystemObject, I notice that there is nothing that looks like it would produce a short file name or a short folder name. The reason is because FileSystemObject returns objects.

To find something that would create a short file or folder name, I need to examine either the file object or the folder object. The file object returns when I use the GetFile method. A folder object returns when I use GetFolder. To investigate this, I use the GetFolder method to retrieve a folder object and examine the members. This is shown in the following image:

Image of command output

Create the short paths

Now that I know how I can shorten the paths, I decide to create a little function to make using the technique a bit easier. The first thing I want to do is to create the FileSystemObject object. So that goes in the BEGIN statement script block as shown here:

Function Get-ShortName

{

 BEGIN { $fso = New-Object -ComObject Scripting.FileSystemObject }

Now I need to process the pipeline information. Is the input object a folder? If it is, I use the GetFolder method as shown here:

PROCESS {

  If ($_.psiscontainer)

   {$fso.getfolder($_.fullname).ShortName}

If it is not a folder, it is more than likely a file. So I use the GetFile method:

 ELSE {$fso.getfile($_.fullname).ShortName} } }

That’s all there is to it. Here is the complete Get-ShortName function:

Function Get-ShortName

{

 BEGIN { $fso = New-Object -ComObject Scripting.FileSystemObject }

 PROCESS {

  If ($_.psiscontainer)

   {$fso.getfolder($_.fullname).ShortName}

  ELSE {$fso.getfile($_.fullname).ShortName} } }

So, how does it work? Well, in my Windows PowerShell ISE, I run the Get-ShortName.ps1 file and load the Get-ShortName function. Now I use the Get-ChildItem cmdlet to pipe some files and folders to the Get-ShortName function. The command and the results are shown in the following image.

Image of command output

SH, that is all there is to using Windows PowerShell to display short file and folder names. Join me tomorrow when I will talk about more cool Windows PowerShell stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

0 comments

Discussion is closed.

Feedback usabilla icon