Bootstrap FreeKB - PowerShell - Get file base name and directory name
PowerShell - Get file base name and directory name

Updated:   |  PowerShell articles

Here is how you can extract the directory name and base name of a file in PowerShell.

$file      = "C:\Users\JohnDoe\example.txt"
$name      = (Get-Item $file).Name
$fullname  = (Get-Item $file).FullName
$dirname   = (Get-Item $file).DirectoryName
$basename  = (Get-Item $file).Basename
$extension = (Get-Item $file).Extension

Write-Host file      = $file
Write-Host name      = $name
Write-Host fullname  = $fullname
Write-Host dirname   = $dirname
Write-Host basename  = $basename
Write-Host extension = $extension

 

Running this PowerShells script should output the following.

file      = C:\Users\JohnDoe\example.txt
name      = example.txt
fullname  = C:\Users\JohnDoe\example.txt
dirname   = C:\Users\JohnDoe\foo
basename  = example
extension = .txt

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter f21f0c in the box below so that we can be sure you are a human.