Trim (or go up) one level of directory tree in powershell variable

powershell

Solution

Getting the parent from a DirectoryInfo object, like Lee suggests, will certainly work. Alternately, if you prefer to use more PowerShellesque commands as opposed to direct .NET Framework calls, you can use PowerShell's built-in Split-Path cmdlet like this:

$source      = "C:\temp\one\two\three"

$destination = Split-Path -Path $source -Parent

# $destination will be a string with the value "C:\temp\one\two"

Problem

Say you have the variable `$source = "C:\temp\one\two\three"` and you want to set `$destination` equal to `$destination = "C:\temp\one\two"` programmatically, how might you do it? The best idea I've had would be to trim that, but is there a better way? Perhaps something like ``` $source = "C:\temp\one\two\three" $dest = "..$source" ```

Original source