Get Full Path of the Files in PowerShell

Rohan Timalsina Dec 21, 2022 Jan 13, 2022
  1. Use Get-ChildItem to Get the Full Path of the Files in PowerShell
  2. Use Select-Object to Get the Full Path of the Files in PowerShell
  3. Use Format-Table to Get the Full Path of the Files in PowerShell
  4. Use the foreach Loop to Get the Full Path of the Files in PowerShell
Get Full Path of the Files in PowerShell

PowerShell has various cmdlets to manage the files on the system. You can create, copy, move, rename, and delete the files using PowerShell.

You can also search the files and check the existence of a file in PowerShell. A file path tells the location of the file on the system. This tutorial will introduce different methods to get the full path of the files in PowerShell.

Use Get-ChildItem to Get the Full Path of the Files in PowerShell

The Get-ChildItem cmdlet displays a list of files and directories on the specified location. You can use the -Recurse parameter to list all files and directories recursively.

It also shows the sub-directories and their files. It is helpful for recursive file search in PowerShell.

Here is an example of recursive files search:

Get-ChildItem -Path C:\New -Filter test.txt -Recurse

The above command searches for the file test.txt on the location C:\New recursively. It checks for all the directories and sub-directories inside the given location and displays the details of the file if found.

Output:

    Directory: C:\New\complex


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        12/16/2021   7:29 AM            198 test.txt

As you can see, the file test.txt is found in the directory C:\New\complex. It displays the Path, Mode, LastWriteTime, Length, and Name of the file.

You can get the full path of the file by piping the command to %{$_.FullName}.

Get-ChildItem -Path C:\New -Filter test.txt -Recurse | %{$_.FullName}

Output:

C:\New\complex\test.txt

You can use *.txt with the -Filter parameter to get all the files having .txt extensions on the location C:\New.

Get-ChildItem -Path C:\New -Filter *.txt -Recurse | %{$_.FullName}

Output:

C:\New\complex\test.txt
C:\New\record\book.txt
C:\New\record\data.txt

Or, you can use this command to get all the files having the .txt extension and their paths on the specified location.

Get-ChildItem -Path C:\New -Recurse | where {$_.extension -eq ".txt"} | %{$_.FullName}

Use Select-Object to Get the Full Path of the Files in PowerShell

A similar method will be used but, we will use the Select-Object cmdlet with the Get-ChildItem command to get the full path of the files in PowerShell. The Select-Object cmdlet selects specified properties of an object or set of objects.

Get-ChildItem -Path C:\New -Filter *.txt -Recurse | Select-Object -ExpandProperty FullName

Output:

C:\New\complex\test.txt
C:\New\record\book.txt
C:\New\record\data.txt

Use Format-Table to Get the Full Path of the Files in PowerShell

Similarly, you can use Format-Table to get the full path of the files in PowerShell. The Format-Table cmdlet formats the output as a table with the selected properties of an object.

Get-ChildItem -Path C:\pc -Filter car.png -Recurse | Format-Table FullName

Output:

FullName
--------
C:\pc\computing\task4\car.png

Use the foreach Loop to Get the Full Path of the Files in PowerShell

The foreach loop is also known as the foreach statement in PowerShell. It is a language construct for looping through a series of values in a collection of arrays, objects, strings, numbers, etc. You can execute one or more commands against each item in an array within the foreach loop.

Here is how you can use foreach loop to get the full path of the files in PowerShell.

Get-ChildItem -path C:\New\*.txt -Recurse | foreach { "$_" }

Output:

C:\New\complex\test.txt
C:\New\record\book.txt
C:\New\record\data.txt

The above command does not work with the -Filter parameter. You can pipe to the Get-Item cmdlet in the middle to use the -Filter parameter.

Get-ChildItem -Path C:\New -Filter *.txt -Recurse | Get-Item | foreach { "$_" }

Output:

C:\New\complex\test.txt
C:\New\record\book.txt
C:\New\record\data.txt

Get-ChildItem has built-in aliases ls, dir, and gci. You can run all the above commands by using any of the handles in place of the Get-ChildItem cmdlet.

For example:

gci -Path C:\New -Filter *.txt -Recurse | %{$_.FullName}

Output:

C:\New\complex\test.txt
C:\New\record\book.txt
C:\New\record\data.txt
Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website

Related Article - PowerShell File