{"id":3125,"date":"2013-08-01T00:01:00","date_gmt":"2013-08-01T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/08\/01\/use-powershell-to-display-short-file-and-folder-names\/"},"modified":"2013-08-01T00:01:00","modified_gmt":"2013-08-01T00:01:00","slug":"use-powershell-to-display-short-file-and-folder-names","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-display-short-file-and-folder-names\/","title":{"rendered":"Use PowerShell to display Short File and Folder Names"},"content":{"rendered":"<p><strong style=\"font-size: 12px\">Summary<\/strong><span style=\"font-size: 12px\">: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to display short file and folder names.<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;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&mdash;this is a hard stop for my current project.<\/p>\n<p>&mdash;SH<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello SH,<\/p>\n<p>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&mdash;and that is always a good thing.<\/p>\n<p style=\"padding-left: 30px\"><strong>Note<\/strong> &nbsp;I want to give credit to Microsoft PFE, <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/chris+wu\/\" target=\"_blank\">Chris Wu<\/a>, who is responsible for much of what is in today&rsquo;s post by answering a question on our internal Windows PowerShell alias.<\/p>\n<h2>Use the old-fashioned CMD prompt<\/h2>\n<p>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.<\/p>\n<p>This is actually a useful technique at times. I run CMD with the<strong> \/c<\/strong> parameter. The <strong>\/c<\/strong> parameter tells CMD to run a command and then exit. The command I want to run is <strong>dir \/x<\/strong>. <strong>Dir<\/strong>, of course, obtains a directory listing, and <strong>\/x<\/strong> provides for extension information. The command is shown here:<\/p>\n<p style=\"padding-left: 30px\">cmd \/c dir \/x<\/p>\n<p>The command and the associated output from the command are shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6433.hsg-8-1-13-01.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6433.hsg-8-1-13-01.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>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.<\/p>\n<h2>Using the FileSystemObject to create short names<\/h2>\n<p>I used the <strong>Scripting.FileSystemObject<\/strong> object (yes, there really is an &ldquo;object object&rdquo; 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 <strong>FileSystemObject<\/strong>, I pipe the object to the <strong>Get-Member<\/strong> cmdlet:<\/p>\n<p style=\"padding-left: 30px\">$fso = New-Object -com scripting.filesystemobject<\/p>\n<p style=\"padding-left: 30px\">$fso | gm -mem method<\/p>\n<p>The resulting output from the command is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2577.hsg-8-1-13-02.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2577.hsg-8-1-13-02.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>When I inspect the methods available from <strong>FileSystemObject<\/strong>, 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 <strong>FileSystemObject<\/strong> returns objects.<\/p>\n<p>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 <strong>GetFile<\/strong> method. A folder object returns when I use <strong>GetFolder<\/strong>. To investigate this, I use the <strong>GetFolder<\/strong> method to retrieve a folder object and examine the members. This is shown in the following image:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0552.hsg-8-1-13-03.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0552.hsg-8-1-13-03.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<h2>Create the short paths<\/h2>\n<p>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 <strong>FileSystemObject<\/strong> object. So that goes in the <strong>BEGIN<\/strong> statement script block as shown here:<\/p>\n<p style=\"padding-left: 30px\">Function Get-ShortName<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;BEGIN { $fso = New-Object -ComObject Scripting.FileSystemObject }<\/p>\n<p style=\"padding-left: 30px\">Now I need to process the pipeline information. Is the input object a folder? If it is, I use the <strong>GetFolder<\/strong> method as shown here:<\/p>\n<p style=\"padding-left: 30px\">PROCESS {<\/p>\n<p style=\"padding-left: 30px\">&nbsp; If ($_.psiscontainer)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; {$fso.getfolder($_.fullname).ShortName}<\/p>\n<p>If it is not a folder, it is more than likely a file. So I use the <strong>GetFile<\/strong> method:<\/p>\n<p style=\"padding-left: 30px\">&nbsp;ELSE {$fso.getfile($_.fullname).ShortName} } }<\/p>\n<p><span style=\"font-size: 12px\">That&rsquo;s all there is to it. Here is the complete <\/span><strong style=\"font-size: 12px\">Get-ShortName<\/strong><span style=\"font-size: 12px\"> function:<\/span><\/p>\n<p style=\"padding-left: 30px\">Function Get-ShortName<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;BEGIN { $fso = New-Object -ComObject Scripting.FileSystemObject }<\/p>\n<p style=\"padding-left: 30px\">&nbsp;PROCESS {<\/p>\n<p style=\"padding-left: 30px\">&nbsp; If ($_.psiscontainer)<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; {$fso.getfolder($_.fullname).ShortName}<\/p>\n<p style=\"padding-left: 30px\">&nbsp; ELSE {$fso.getfile($_.fullname).ShortName} } }<\/p>\n<p>So, how does it work? Well, in my Windows PowerShell ISE, I run the <strong>Get-ShortName.ps1<\/strong> file and load the <strong>Get-ShortName<\/strong> function. Now I use the <strong>Get-ChildItem<\/strong> cmdlet to pipe some files and folders to the <strong>Get-ShortName<\/strong> function. The command and the results are shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1205.hsg-8-1-13-04.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1205.hsg-8-1-13-04.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>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.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><span style=\"font-size: 12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to display short file and folder names. &nbsp;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 [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[38,11,3,12,45],"class_list":["post-3125","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-folders","tag-scripting-guy","tag-storage","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to display short file and folder names. &nbsp;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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=3125"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3125\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=3125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=3125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=3125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}