{"id":10131,"date":"2006-06-24T15:00:00","date_gmt":"2006-06-24T15:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2006\/06\/24\/basename-for-fileinfo-objects\/"},"modified":"2019-02-18T13:21:37","modified_gmt":"2019-02-18T20:21:37","slug":"basename-for-fileinfo-objects","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/basename-for-fileinfo-objects\/","title":{"rendered":"BaseName for FileInfo objects"},"content":{"rendered":"<p>&lt;Edited to add categories&gt;<br \/>In our active, responsive, and useful newsgroup Microsoft.Public.Windows.PowerShell (SELL SELL SELL \ud83d\ude42&nbsp;),&nbsp; MVP Alex Angelopoulos recented posted the following:<\/p>\n<blockquote>\n<p><font face=\"Garamond\">Although file extension changing is a common technique in administrative <br \/>tasks, the System.IO.FileInfo class does not provide a direct Basename <br \/>property and neither does PowerShell.<\/p>\n<p>This is useful enough that I believe it should be added to PS typedata. It <br \/>can&nbsp; be done with a simple ScriptProperty, as shown below:<\/p>\n<p>&nbsp; &lt;Type&gt;<br \/>&nbsp;&nbsp; &lt;Name&gt;System.IO.FileInfo&lt;\/Name&gt;<br \/>&nbsp;&nbsp; &lt;Members&gt;<br \/>&nbsp;&nbsp;&nbsp; &lt;ScriptProperty&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp; &lt;Name&gt;Basename&lt;\/Name&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp; &lt;GetScriptBlock&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $this.Name.Remove($this.Name.Length &#8211; $this.Extension.Length);<br \/>&nbsp;&nbsp;&nbsp;&nbsp; &lt;\/GetScriptBlock&gt;<br \/>&nbsp;&nbsp;&nbsp; &lt;\/ScriptProperty&gt;<br \/>&nbsp;&nbsp; &lt;\/Members&gt;<br \/>&nbsp; &lt;\/Type&gt;<\/font><\/p>\n<\/blockquote>\n<p>Alex posted this as a feature request and asked people to comment &amp; vote on it at: <a href=\"https:\/\/connect.microsoft.com\/feedback\/ViewFeedback.aspx?FeedbackID=145875&amp;SiteID=99\">https:\/\/connect.microsoft.com\/feedback\/ViewFeedback.aspx?FeedbackID=145875&amp;SiteID=99<\/a><\/p>\n<p>Alex&#8217;s point is very timely in that we were just discussing this yesterday (Actually Alex tends to do that a lot, I wonder if my office is bugged.&nbsp; Hmmmm.).&nbsp; Anyway, we were talking about the possibility of adding this as an operator and then maybe having user-define operators.&nbsp; Of course my initial reaction was &#8211; let&#8217;s get this into V1.0 but we at a point in the project where we need to stop making changes to ensure the stability of the codebase.&nbsp; (I&nbsp;suspect that our GPM Hilal has secretly purchased a roll of duct-tape for use the next time someone suggests adding a feature to V1.0. )<\/p>\n<p>So what is great is that this is EXACTLY the situation we designed Windows PowerShell to address.&nbsp; I&#8217;ve always HATED the conversation where a customer tells me what they need and then I have to say, &#8220;Got it, we&#8217;ll add that.&nbsp; Just hang in there a couple more years and we&#8217;ll get you what you need.&#8221;&nbsp; We designed the system to address this (As you can see from the mention of user-defined operators, we continue to think about how to do a better job of this).&nbsp; You can take the XML that Alex provided above and include it into your own My.Types.ps1xml (this process is describe reasonably well in <a href=\"http:\/\/blogs.msdn.com\/powershell\/archive\/2006\/06\/24\/644987.aspx\">http:\/\/blogs.msdn.com\/powershell\/archive\/2006\/06\/24\/644987.aspx<\/a>&nbsp;).&nbsp;<\/p>\n<p>I did this and now I can do the following:<\/p>\n<p>PS&gt; dir m* |ft name,basename -auto<\/p>\n<p><font face=\"Courier New\" size=\"1\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Basename<br \/>&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8211;<br \/>mines.ps1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mines<br \/>MonadScripting-HandsOnLab.doc MonadScripting-HandsOnLab<br \/>mow.types.ps1xml&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mow.types<br \/>msh.msc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; msh<br \/>MSH.xshd&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MSH<br \/>MSH.xshd.1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MSH.xshd<br \/>MSH.xshd.orig&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MSH.xshd<br \/>MSNSearchService.cs&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MSNSearchService<br \/>MSNSearchService.dll&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MSNSearchService<br \/>my.format.ps1xml&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; my.format<br \/>my.TaskTypes.ps1xml&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; my.TaskTypes<br \/>my.types.ps1xml&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; my.types<br \/>my.types2.ps1xml&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; my.types2<br \/>my1.format.ps1xml&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; my1.format<br \/>MyKnowledge.ps1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MyKnowledge<br \/>MyTabExpansion.ps1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MyTabExpansion<\/font><\/p>\n<p>&nbsp;So &#8211; we&#8217;ll almost certainly do something similar to this in V2.0 (maybe V1.1) but you don&#8217;t have to wait for us &#8211; you can have this function today.&nbsp; Now just be aware of one thing &#8211; name collisions.&nbsp; If you think about it, when we get around to it, if we decide to add this function to the type (as opposed to creating an operator for it), we&#8217;ll almost certainly call it &#8220;BASENAME&#8221;.&nbsp; If you&#8217;ve already define your own property called BASENAME, there will be a collision (we use Casino rules on name collision [House wins on a tie]).&nbsp; So you might consider calling it something unique.&nbsp; <\/p>\n<p>We should probably&nbsp;promulgate&nbsp;a naming convention for user-defined extensions.&nbsp; I&#8217;d love to get input on that topic so please post that as comments to this blog.&nbsp; <\/p>\n<p>Enjoy!<br \/>Jeffrey Snover<br \/>Windows PowerShell Architect<\/p>\n<p>PSMDTAG:PHILOSOPHY: Provide mechanisms that the community can solve problems without waiting till the next release cycle.<br \/>PSMDTAG:TYPEEXTENSION: BaseName on FileInfo<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&lt;Edited to add categories&gt;In our active, responsive, and useful newsgroup Microsoft.Public.Windows.PowerShell (SELL SELL SELL \ud83d\ude42&nbsp;),&nbsp; MVP Alex Angelopoulos recented posted the following: Although file extension changing is a common technique in administrative tasks, the System.IO.FileInfo class does not provide a direct Basename property and neither does PowerShell. This is useful enough that I believe it [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[11,15],"class_list":["post-10131","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-philosophy","tag-typeextension"],"acf":[],"blog_post_summary":"<p>&lt;Edited to add categories&gt;In our active, responsive, and useful newsgroup Microsoft.Public.Windows.PowerShell (SELL SELL SELL \ud83d\ude42&nbsp;),&nbsp; MVP Alex Angelopoulos recented posted the following: Although file extension changing is a common technique in administrative tasks, the System.IO.FileInfo class does not provide a direct Basename property and neither does PowerShell. This is useful enough that I believe it [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/10131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=10131"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/10131\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=10131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=10131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=10131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}