How to Get the Path of a File in PowerShell?

Recently, while working with an automation script in PowerShell, I got a requirement to get the path of a file in PowerShell. There are various methods to get the path of a file in PowerShell.

To get the path of a file in PowerShell, you can use the Get-Item cmdlet followed by the FullName property to retrieve the full path of a single file. For example, (Get-Item “C:\Path\To\Your\File.txt”).FullName will return the complete path of File.txt. If you need paths of multiple files, use Get-ChildItem -Path “C:\Path\To\Directory” -Recurse | Select-Object -ExpandProperty FullName to list the full paths of all files within a directory and its subdirectories.

Get the Path of a File in PowerShell

1. Using Get-ChildItem

The Get-ChildItem cmdlet in PowerShell is a very useful command that can be used to list items in one or more specified locations. If you want to retrieve the full path of all files within a specific directory, you can use Get-ChildItem combined with other cmdlets.

Get-ChildItem -Path "C:\MyFolder\" -Recurse | Select-Object -ExpandProperty FullName

The -Recurse parameter is used to get items in all child directories of the specified path. The Select-Object cmdlet with the -ExpandProperty parameter is then used to extract the full path information.

I executed the code using VS code, and you can see the output in the screenshot below:

Get the Path of a File in PowerShell

2. Using Resolve-Path

You can use the cmdlet if you have a relative path and need to resolve it to a full path. This cmdlet is particularly useful when you’re working with paths that may include wildcards or aliases.

Resolve-Path -Path "Relative\Path\To\Your\File.txt"

This will output the fully resolved path to the file.

3. Get the Path of the Currently Executing Script

Sometimes, you may need to find the path of the script that is currently running. You can achieve this by using the built-in $PSScriptRoot variable, which automatically contains the full path to the directory from which the script is being run.

$ScriptPath = $PSScriptRoot
Write-Host "The script is located in: $ScriptPath"

This is a straightforward way to retrieve the execution path without additional cmdlets.

You can see the screenshot below for the output:

How to Get the Path of a File in PowerShell

4. Using Split-Path

The Split-Path cmdlet is another tool you can use to handle path strings in PowerShell. If you have a full path to a file and you want to extract just the directory part, Split-Path is the cmdlet to use.

$FilePath = "C:\MyFolder\File.txt"
$DirectoryPath = Split-Path -Path $FilePath
Write-Host "The file is located in: $DirectoryPath"

This will display the directory part of the file path, excluding the file name.

You can see the output in the screenshot below:

PowerShell Get the Path of a File

5. Combining Get-ChildItem and Split-Path

For a more complex example, you can combine Get-ChildItem and Split-Path to list all files within a directory and then output their respective directory paths.

Get-ChildItem -Path "C:\Your\Target\Directory" -Recurse | ForEach-Object {
    $DirectoryPath = Split-Path -Path $_.FullName
    Write-Host "File: $($_.Name) is in directory: $DirectoryPath"
}

This script lists each file found in the target directory and its subdirectories, along with the full path of the directory in which each file resides.

6. Using Get-Location

If you simply want to know the current directory you’re working in, Get-Location will provide that information. This cmdlet returns the current working directory path.

$CurrentDirectory = Get-Location
Write-Host "The current directory is: $CurrentDirectory"

7. Using Custom Functions

For advanced users, PowerShell allows the creation of custom functions to tailor the retrieval of paths to specific needs. For example, you can create a function that filters and formats the output of file paths based on certain criteria.

function Get-FilePaths {
    param(
        [string]$Directory,
        [string]$Filter = "*.*"
    )
    Get-ChildItem -Path $Directory -Filter $Filter -Recurse | ForEach-Object {
        $_.FullName
    }
}

# Usage
Get-FilePaths -Directory "C:\Your\Target\Directory" -Filter "*.txt"

This custom function Get-FilePaths allows you to specify a directory and a filter for the file types you’re interested in. It then lists the full paths of all files that match the filter.

Conclusion

PowerShell provides multiple ways to retrieve file paths, each suited to different scenarios. Whether you’re looking for a simple way to get the current directory, need to resolve a relative path, or require a list of all files in a directory with their full paths, PowerShell has different methods to achieve this.

In this PowerShell tutorial, I have explained how to get the file path in PowerShell using different methods.

You may also like: