<<< Back to Search

File Management - Get Directory Folder File Path - Parts

Get Get Directory Folder File Path - Parts from a giver Path/File.

Snippet and Sample

Copy text below into a PowerShell session and execute to see the code in action.

# File Management - Get Directory Folder File Path - Parts -_-
# Snippet and Sample
# www.PowerShellExamples.com


# PARTS FROM A GIVEN FILE -_-

$FileName = "FileNameExample.txt"
Write-Host "File Name Example: `r`n$FileName`r`n" -ForegroundColor DarkCyan

# Get File Name Without Extension -_-
$FileNameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($FileName)
Write-Host "Get File Name Without Extension: `r`n$FileNameWithoutExtension`r`n"

# Get Extension -_-
$FileExtension = [System.IO.Path]::GetExtension($FileName)
Write-Host "Get Extension: `r`n$FileExtension`r`n"



# PARTS FROM A GIVEN PATH/DIRECTORY/FOLDER -_-

$Path = "C:\Users\Public\Documents\MyFile.txt"
Write-Host "Path/Directory/Folder Example: `r`n$Path`r`n" -ForegroundColor DarkCyan

# Get File Name (using [System.IO.Path]) -_-
$FileName = [System.IO.Path]::GetFileName($Path)
Write-Host "Get File Name (using [System.IO.Path]): `r`n$FileName`r`n"

# Get File Name (using Split-Path) -_-
$FileName = split-path -Path $path -Leaf
Write-Host "Get File Name (using Split-Path): `r`n$FileName`r`n"

# Get File Name Without Extension -_-
$FileNameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($Path)
Write-Host "Get File Name Without Extension: `r`n$FileNameWithoutExtension`r`n"

# Get Parent Path/Directory/Folder (using [System.IO.Path]) -_-
$ParentFolder = [System.IO.Path]::GetDirectoryName($Path)
Write-Host "Get Parent Path/Directory/Folder (using [System.IO.Path]): `r`n$ParentFolder`r`n"

# Get Parent Path/Directory/Folder (using Split Path) -_-
$ParentFolder = split-path -Path $path -Parent
Write-Host "Get Parent Path/Directory/Folder (using Split Path): `r`n$ParentFolder`r`n"    


Result

PS C:\>

File Name Example:
FileNameExample.txt

Get File Name Without Extension:
FileNameExample

Get Extension:
.txt


Path/Directory/Folder Example:
C:\Users\Public\Documents\MyFile.txt

Get File Name (using [System.IO.Path]):
MyFile.txt

Get File Name (using Split-Path):
MyFile.txt

Get File Name Without Extension:
MyFile

Get Parent Path/Directory/Folder (using [System.IO.Path]):
C:\Users\Public\Documents

Get Parent Path/Directory/Folder (using Split Path):
C:\Users\Public\Documents