Warren  P
Warren P

Reputation: 69114

How can I get just the directory name from a variable that includes a fully qualified filename in Powershell?

In powershell this variable is the path to the profile: $Profile

On my system it equals:

  C:\Users\Warren\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

I would like to get just the path part:

  C:\Users\Warren\Documents\WindowsPowerShell\

In C# you would call Path.GetDirectoryName, so I guess I can call [system.io.Path]::GetDirectoryName($fileName) but I'm wondering if there's a builtin path manipulation system I should know about in PowerShell?

Is this what people do?

   [system.io.Path]::GetDirectoryName($Profile)

Upvotes: 1

Views: 187

Answers (1)

Shay Levy
Shay Levy

Reputation: 126932

Use the Split-Path cmdlet:

Split-Path $PROFILE -Parent

Upvotes: 3

Related Questions