How To Find a File’s Path in PowerShell

These methods ensure that your PowerShell scripts can run consistently on different computers, even when the file locations are unknown or may vary from one system to another.

Brien Posey

October 31, 2023

3 Min Read
closeup view of a keyboard with bright led lights
Alamy

When writing about PowerShell, one of the toughest things is ensuring that a script runs consistently on different computers. A script may not necessarily run the same way on someone else’s computer as it does on mine. Oftentimes, this problem can arise due to the requirement for external dependencies.

For instance, if a script requires loading a module or calling on an external data file or other dependency, you need to specify the path and filename in PowerShell. Herein lies the problem: a file’s location on my computer doesn’t guarantee it’s the same place on yours. As such, a hardcoded path can lead to issues.

The only reliable way to ensure a script can find the dependency files is by using PowerShell to verify the physical location of those dependencies. To illustrate this, I would like to share a couple of methods I sometimes use.

Locate and Read a File When Its Location Is Unknown

For this article, let’s imagine we need to create a PowerShell script that reads a text file named “Example.txt” and displays its contents. Normally, you can simply enter the Get-Content cmdlet, followed by the file’s path and name. However, just to make things interesting, let’s assume we have no idea where the file is located on the hard disk.

In this scenario, the first step is to use the Get-ChildItem command to locate the file. Here are the commands I would use to find the file:

Set-Location C:$File=Get-ChildItem Example.txt -Recurse -ErrorAction SilentlyContinue

The first command, “Set-Location C:,” instructs PowerShell to navigate to the root folder. The second command uses the Get-ChildItem cmdlet to search for the file in question. By using the Recurse parameter, we indicate to PowerShell that we want to search within any subfolders, as well.

We include the ErrorAction parameter because there may be folders that PowerShell lacks access to. When the Get-ChildItem cmdlet encounters one of these folders, it typically generates an error message. However, by setting the ErrorAction to SilentlyContinue, we can suppress these errors and produce a cleaner output.

When the two lines of code above finish executing, the variable $File should now contain information about the file’s location. The file’s physical location is stored in an attribute called DirectoryName. As such, we can copy the DirectoryName attribute to another variable, which I’ll name $FileLocation. The command I use to extract the file location is as follows:

$FileLocation = $File.DirectoryName

PowerShell screenshot showing the $FileLocation variable now contains the file’s path

File Location 1

Figure 1. The $FileLocation variable now contains the file’s path.

So, with the file's location stored in the $FileLocation variable, let’s get back to the task of reading and displaying the Example.txt file. There are a few different ways that I could do this. The simplest option would likely be to extract the file’s full name (which includes both the path and filename) from the $File variable. Then, I could use the Get-Content command to read the file.

Here is what that sequence of commands might look like:

Set-Location C:$File=Get-ChildItem Example.txt -Recurse -ErrorAction SilentlyContinue$FileLocation = $File.FullNameGet-Content $FileLocation

PowerShell screenshot showing the file’s contents without knowing the file’s location

File Location 2

Figure 2. I have displayed the file’s contents despite the file’s unknown location.

Use PowerShell’s Location Stack

Another way to accomplish this task involves using PowerShell’s location stack. If you refer to the code block shown a moment ago, you will notice that the very first line used the Set-Location cmdlet. This cmdlet lets you direct the PowerShell prompt to a location stored in a variable. Therefore, you can use a command like Set-Location $FileLocation. Once executed, you can access the file without needing to specify a path since the PowerShell prompt is already directed to the correct location.

To illustrate how this works, check out the following code sample:

Set-Location C:$File=Get-ChildItem Example.txt -Recurse -ErrorAction SilentlyContinue$FileLocation = $File.DirectoryNameSet-Location $FileLocationGet-Content Example.txt

PowerShell screenshot showing code used to read a file from an unknown location

File Location 3

Figure 3. This is another way that you can read a file from an unknown location.

About the Author

Brien Posey

Brien Posey is a bestselling technology author, a speaker, and a 20X Microsoft MVP. In addition to his ongoing work in IT, Posey has spent the last several years training as a commercial astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space.

http://brienposey.com/

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like