Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Shorten a string in PowerShell

I have a Question about shortening a string in PowerShell. So for example we have this code here:
$path = "C:\Pow\temp\temp2"

Now, I wanted to shorten this String, even if I do not know it, but what I know is that it is a Path. So from this Path I want to get only the name of the last folder so from ‘temp2’. Does someone know with what PowerShell Code you can do that? I would be happy for an answer, thank you.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In PowerShell 3.0 and newer, use Split-Path:

PS ~> $path = "C:\Pow\temp\temp2"
PS ~> Split-Path $path -Leaf
temp2

In PowerShell 2.0, use Path.GetFileName():

PS ~> [System.IO.Path]::GetFileName($path.TrimEnd('\'))
temp2
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading