Programming

Get full path of the files in PowerShell

19 September 2026 · 10 min read

Get full path of the files in PowerShell

PowerShell, a versatile scripting language for Windows, offers numerous ways to manage files and directories. Often, when working with scripts, you need to get full path of the files in PowerShell to ensure your code accurately targets the intended resources. Understanding how to retrieve the complete path – including the drive letter and all parent directories – is essential for robust scripting. This avoids ambiguity and ensures that PowerShell can correctly locate and manipulate the files you’re working with, regardless of the script’s execution context. Mastering these techniques will significantly improve the reliability and portability of your PowerShell scripts, making them less prone to errors caused by relative path issues. This article will walk you through various methods for achieving this, complete with examples and explanations.

Understanding Path Types in PowerShell

Before diving into the specific commands, it’s crucial to understand the difference between relative and absolute paths. A relative path is defined in relation to the current working directory. For instance, if you’re in “C:\Scripts” and a file is in “C:\Scripts\Data\file.txt”, the relative path might be “.\Data\file.txt”. An absolute path, also known as a full path, specifies the exact location of the file from the root directory, like “C:\Scripts\Data\file.txt”. Using absolute paths in your PowerShell scripts ensures that the script will find the correct file regardless of the directory from which it is run. This is especially important when dealing with scheduled tasks or scripts that are executed from different locations.

PowerShell handles paths as strings, but it also provides cmdlets and methods to work with paths in a more structured way. These tools allow you to easily convert between relative and absolute paths, check if a path exists, and manipulate path components. Utilizing these features enhances the readability and maintainability of your scripts. Consider the impact of differing execution environments. A path that works fine in your development environment may fail in a production environment if the working directory is different. Therefore, explicitly resolving paths to their full form is a best practice for creating reliable PowerShell scripts. According to Microsoft’s documentation on file system objects (Microsoft Docs), understanding these path types is foundational to effective file management in PowerShell.

Here are a few key takeaways about path types:

  • Relative paths are convenient for simple scripts but can be error-prone.
  • Absolute paths offer greater reliability and portability.
  • PowerShell provides tools to easily convert between the two.

Methods to Get Full Path in PowerShell

PowerShell offers several methods to get full path of the files in PowerShell. The most common include using the Get-Item, Get-ChildItem, and .FullName property. Get-Item retrieves a single file or directory, while Get-ChildItem retrieves multiple items. The .FullName property then provides the absolute path. Another approach involves using the Resolve-Path cmdlet, which directly converts a relative path to an absolute path. Each method has its own use cases, and understanding their nuances will allow you to choose the most appropriate tool for the job.

Let’s look at some code examples. The Get-Item cmdlet, when combined with the .FullName property, is a straightforward way to get the full path of a specific file. For example: (Get-Item “.\MyFile.txt”).FullName will return the full path of “MyFile.txt” relative to the current directory. Similarly, Get-ChildItem can be used to retrieve the full paths of multiple files in a directory. For instance, (Get-ChildItem -Path “.\Data” -File).FullName lists the full paths of all files within the “Data” subdirectory. The Resolve-Path cmdlet is particularly useful when you have a path string that might be relative and you need to ensure it’s absolute before proceeding with further operations. For instance: Resolve-Path “.\MyFile.txt” will resolve the relative path to its absolute equivalent. According to a 2023 survey by Stack Overflow (Stack Overflow Developer Survey), PowerShell remains a popular scripting language for system administrators, making these path manipulation skills highly valuable.

Here’s a summary of the methods:

  • Get-Item: Ideal for retrieving the full path of a single, known file.
  • Get-ChildItem: Useful for retrieving full paths of multiple files in a directory.
  • Resolve-Path: Best for converting relative paths to absolute paths.

Using Get-Item and .FullName

The Get-Item cmdlet retrieves a specific item, such as a file or directory. When you pipe the output of Get-Item to the .FullName property, you obtain the full path of that item. This is a simple and direct method for getting the absolute path of a known file. For example, if you have a file named “report.txt” in the current directory, you can use the command (Get-Item “report.txt”).FullName to retrieve its full path. This method is especially useful when you’re working with a single file and need its absolute path for further processing. Remember that Get-Item will throw an error if the specified file does not exist, so it’s good practice to wrap the command in a try-catch block for error handling.

Consider a scenario where you need to log the full path of a configuration file used by a PowerShell script. You can use Get-Item to retrieve the file object and then access its .FullName property to get the absolute path. This path can then be written to a log file for auditing purposes. For example:

try { $configFile = Get-Item "config.ini" $configFilePath = $configFile.FullName Write-Host "Configuration file path: $configFilePath" } catch { Write-Host "Error: Configuration file not found." } 

Using Get-ChildItem and .FullName

The Get-ChildItem cmdlet retrieves a list of files and directories within a specified path. Similar to Get-Item, you can pipe the output of Get-ChildItem to the .FullName property to obtain the full paths of each item in the list. This method is particularly useful when you need to process multiple files in a directory and require their absolute paths. For instance, if you want to get the full paths of all text files in a directory named “Logs”, you can use the command (Get-ChildItem -Path “Logs\.txt”).FullName. This will return an array of full paths, one for each text file in the “Logs” directory.

This is the paragraph optimized for a featured snippet: To get full path of the files in PowerShell using Get-ChildItem, simply use the command Get-ChildItem -Path “YourDirectory” | Select-Object -ExpandProperty FullName. Replace “YourDirectory” with the actual path to the directory containing the files. This command retrieves all items in the specified directory and then extracts only the full path for each item, providing a clean list of absolute file paths that can be used in subsequent script operations. This is especially useful when you need to process multiple files programmatically.

Using Resolve-Path

The Resolve-Path cmdlet is specifically designed to resolve relative paths to absolute paths. It takes a path string as input and returns the corresponding absolute path. This cmdlet is useful when you’re working with paths that might be relative to the current working directory and you need to ensure that they are absolute before proceeding. For example, if you have a script that accepts a file path as a parameter, you can use Resolve-Path to convert the input path to an absolute path, regardless of whether the user provided a relative or absolute path. The command Resolve-Path “.\data\input.txt” will return the full path of “input.txt” relative to the current directory.

Practical Examples and Use Cases

To illustrate the practical applications of these methods, consider a scenario where you need to create a backup script that archives files from a specific directory to a remote server. The script needs to iterate through the files in the source directory, compute their checksums, and then copy them to the backup location. To ensure that the script can reliably locate the files, you would use Get-ChildItem to retrieve the list of files and then access their .FullName property to get their absolute paths. These absolute paths would then be used to compute the checksums and copy the files to the backup server. This approach ensures that the script will work correctly regardless of the current working directory.

Another common use case is when you need to process log files from different applications. Each application might store its log files in a different directory, and the paths to these directories might be specified in a configuration file. Your script could read the configuration file, retrieve the paths to the log directories, and then use Resolve-Path to convert these paths to absolute paths. This would allow your script to process the log files regardless of the location of the configuration file or the current working directory. Understanding file paths is crucial for scripting.

Here’s a simple example of how you might use Resolve-Path in a log processing script:

$logDirectory = ".\Logs" Could be read from a config file $absoluteLogDirectory = Resolve-Path $logDirectory Write-Host "Processing log files in: $absoluteLogDirectory" 

Best Practices for Handling File Paths in PowerShell

When working with file paths in PowerShell, it’s important to follow some best practices to ensure the reliability and maintainability of your scripts. Always use absolute paths whenever possible to avoid ambiguity and ensure that your scripts will work correctly regardless of the current working directory. Use the Test-Path cmdlet to verify that a file or directory exists before attempting to access it. This can prevent errors and improve the robustness of your scripts. Use proper error handling techniques, such as try-catch blocks, to handle potential exceptions that might occur when working with file paths.

Additionally, consider using the Join-Path cmdlet to construct file paths programmatically. This cmdlet ensures that the path separators are correctly formatted for the operating system on which the script is running. Avoid hardcoding path separators (e.g., “\” or “/”) in your scripts, as this can make them less portable. When working with user input, always validate the input to ensure that it’s a valid file path and that the user has the necessary permissions to access the file or directory. According to the SANS Institute (SANS Institute), proper input validation is crucial for preventing security vulnerabilities in PowerShell scripts.

  1. Always use absolute paths.
  2. Use Test-Path to verify existence.
  3. Implement proper error handling.
  4. Use Join-Path for constructing paths.

FAQ: Getting Full File Paths in PowerShell

How do I get the full path of a file in the current directory?
You can use the command `(Get-Item "filename.txt").FullName`, replacing "filename.txt" with the actual name of your file.
How can I retrieve the full paths of all files in a directory?
Use the command `Get-ChildItem -Path "directorypath" | Select-Object -ExpandProperty FullName`, replacing "directorypath" with the path to your directory.
What is the difference between Get-Item and Get-ChildItem?
Get-Item retrieves a specific item (file or directory), while Get-ChildItem retrieves a list of items within a specified path.
How do I handle errors if a file doesn't exist?
Wrap your command in a `try-catch` block. For example: `try { (Get-Item "nonexistentfile.txt").FullName } catch { Write-Host "File not found" }`.
Can I use wildcards with Get-ChildItem?
Yes, you can use wildcards like `.txt` to retrieve all files with a specific extension. For example: `Get-ChildItem -Path "directorypath\.txt" | Select-Object -ExpandProperty FullName`.
By mastering these techniques and adhering to best practices, you can confidently **get full path of the files in PowerShell** and ensure the reliability and portability of your PowerShell scripts. Understanding these methods not only enhances your scripting capabilities but also **Question & Answer :** I need to get all the files including the files present in the subfolders that belong to a particular type.

I am doing something like this, using Get-ChildItem:

Get-ChildItem "C:\windows\System32" -Recurse | where {$_.extension -eq ".txt"} 

However, it’s only returning me the files names and not the entire path.

Add | select FullName to the end of your line above. If you need to actually do something with that afterwards, you might have to pipe it into a foreach loop, like so:

get-childitem "C:\windows\System32" -recurse | where {$_.extension -eq ".txt"} | % { Write-Host $_.FullName }