PowerShell- Get Full Path of File

Get Full path of file in PowerShell

Get Full Path of File in PowerShell

To get full path of file in powershell:

  • Go to folder where file is present. You can go to any parent folder as well.
  • Use Get-ChildItem with -Recurse option.
  • Pipe it to where-object cmdlet to filter filename. In this example, we have given filename as test.ps1
  • Pipe it to forEach-Object cmdlet and get FullName property of current object.

Here are multiple examples to get Full Path of Files in directory in PowerShell. Each option will use different combinations with Get-ChildItem cmdlet.

Using Get-ChildItem Cmdlet:

To get the full path of files in directory, use the Get-ChildItem cmdlet in PowerShell.

In the above code, the Get-ChildItem cmdlet was used to get all the full names of the files available in the D:\PowerShell\Testing directory and its subdirectory. Here, the -Path parameter may specify the directory path from which we want to get the path of the files.

Let’s see all the parameters used in the above command one by one to understand it:

  • The -Recurse parameter informs the PowerShell to recursively search the D:\PowerShell\Testing directory and all its subdirectories.
  • | is called a pipeline operator; it can pass the previous command’s output as the input to the next command. In this example, it passed all the names of the files in the D:\PowerShell\Testing directory to this %{$_.FullName} command.
  • % is used as an alias of the ForEach-Object cmdlet
  • {$_.FullName} shows the action we want to take on the selected object. As the name suggests, we want to get the full name of all the files in the specified directory.

Now let’s consider a scenario in which we must extract all the .txt files from the specified directory. For this, look at the below example:

In this example, the -Filter parameter is used with the Get-ChildItem cmdlet to filter all the .txt files available in the directory D:\PowerShell\Testing and its subdirectories.

To get full path of file in current directory, use Get-ChildItem without path attribute.

Using ForEach-Object Cmdlet with Get-ChildItem

To get the full path of files in a directory, use the Get-ChildItem cmdlet with the ForEach-Object cmdlet in PowerShell.

In this example, the Get-ChildItem was used with the ForEach-Object to filter all the files with the .py extension available in the specified folder and its sub-folders. Here the Get-ChildItem recursively got the names of all files and passed them to the ForEach-Object cmdlet to get the full path of each file in the directory.

Suppose you want to get the full path of the files with the size of files in the given directory and its sub-directories.

We can observe the path of all files having the .txt extension with their size. Here, the $_.Length parameter was used to get the length/size of each file.

Using Select-Object Cmdlet with Get-ChildItem

To retrieve the full path of files in a directory, use the Get-ChildItem with the Select-Object cmdlet in PowerShell.

In the above command, Select-Object was used with Get-ChildItem. It is another way to get the path of files available in the specified directory. Here, Select-Object was used to select the object’s specific property, which was ExpandProperty FullName in our case. This was used to expand the FullName property of the object to get the full path of the files available in the given directory and its subdirectories which was D:\PowerShell\Testing in the above example.

Using Format-List Cmdlet with Get-ChildItem

To access the full path of files in a directory, use the Get-ChildItem cmdlet with Format-List in PowerShell.

The Format-List cmdlet was used in the above command to get the output as a list. The above example got the path of the files with the .txt extension available in the D:\PowerShell\Testing directory and its subdirectories. The above output is formatted and written FullName: at the start of each file path.

Let’s see another example to get all the files with the .docx extension.

This code is similar to the previous one. However, this time, we got the path of all files with the .docx extension.

Using Format-Table Cmdlet with Get-ChildItem

To get the files’ full path in a directory, use the Get-ChildItem cmdlet with the Format-Table cmdlet in PowerShell.

In the above PowerShell snippet, the Format-Table cmdlet was used along with the Get-ChildItem cmdlet to get the full path of the files with the .txt extension available in the specified directory and its subdirectory. Due to using the Format-Table cmdlet, the output was formatted as a table with the complete path to each file displayed. And the -AutoSize parameter was used to adjust the column’s width to fit the data.

That’s all about how to get full path of file in PowerShell.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *