PowerShell can help manage all the files on the system using a variety of commands. This file management operation includes copying, moving, deleting, or renaming. PowerShell also helps search files on the system and can be utilized to find the location of the existing file. Moreover, it can also assist in getting the path of multiple files at once. For this purpose, the “Get-ChildItem” command is majorly utilized to retrieve or get the file path.
This tutorial will present a complete guide to get or find the path of the files in PowerShell.
How to Get/Retrieve the Full Path of the Files Using PowerShell?
These enlisted methods can be utilized to get the full path of the files in PowerShell in combination with the “Get-ChildItem” command:
Method 1: Get Full Path of the Files in PowerShell Using “ForEach” Command
The “ForEach” cmdlet is when used with the “Get-ChildItem”, it gets and prints the full path of the files.
Example
Let’s consider a given code example:
According to the given command:
- First, invoke the “Get-ChildItem” command to get the files from a specified location or directory.
- Next, use the “-Recurse” parameter to look for the files in the sub-folders.
- After that, add the pipeline “|” that takes the output from the previous command and then transfers it as input into the next command.
- Finally, add a “foreach” loop to print the full path of the files one by one:
The full paths of all of the files of the selected directory have been displayed on the console.
Method 2: Get/Retrieve Full Path of the Files in PowerShell Using “Select-Object” Command
Another command that can be when used with the “Get-ChildItem” to retrieve the path/directory of the files is the “Select-Object”.
Example
To utilize it, check out the given command:
In the stated code:
- “Select-Object” command is used to select the objects specified by the user.
- “-ExpandProperty” selects and expands the specified property if the property is an array.
- “FullName” is used to get the full name of the specified object:
Method 3: Get Full Path of the Files in PowerShell Using “Format-Table” Command
The “Format-Table” cmdlet assists in formatting the output into a table with only selected properties of an object. More specifically, it prints out the file paths in the form of a table when used with the “Get-ChildItem” command.
Example
Run the following line of code in the PowerShell console:
The above command will search and get the files with the “.txt” extension that are present within the “C:\Doc” directory. For that purpose, we have used the “-Filter” parameter and added the wild card “*” operator alongside the “.txt” extension to get only the files with the mentioned extension:
As can be seen from the above output that the path of the files has been printed in the table format.
Conclusion
The full path of the files using PowerShell can be retrieved using the “Get-ChildItem” cmdlet. Moreover, some other commands could be used with the specified cmdlet to get the file’s path in multiple ways. These commands include “Select-Object”, “Format-Table”, or “ForEach”. This tutorial has presented a thorough guide to get the files in PowerShell.