PowerShell – Get Filename from Path

PowerShell get filename for Path

PowerShell – Get Filename From Specified Path

The location of a file on a system can be determined by its path. In PowerShell, there are several ways to get filename from path. Firstly, it’s essential to understand what is in a path before going into depth.

What is in a Path?

A path indicates the location of an item in a particular format. For instance, a file’s path could be C:\folder\subfolder\fileName.txt . The path includes the following parts if you separate it into different sections:

  • C:\ is the drive, the qualifier, or the specified path. A qualifier starts at the left of the path and ends with a co#lon (:).
  • \folder\subfolder\ represents the folder and its subfolder(s) or containers.
  • fileName.txt is the name of the file or leaf. The leaf is the last part or element of a path.
  • .txt is the extension of a file.

Using Split-Path Cmdlet

Use the Split-Path command with the -Leaf parameter to get filename from path in PowerShell.

In PowerShell, the Split-Path cmdlet returned the specified portion of a given path. As we have seen in the previous section, a path’s elements can only be the parent folder, subfolder, file name, or file extension.

In the above example, the Split-Path cmdlet breakdown the given path C:\Intel\project\ConvertString.ps1 in separate parts. We used the -Leaf parameter above to extract the filename with the extension; as we can see, the ConvertString.ps1 is the file name in the given path.

Note that we can also get file names without extension using the Split-Path command with the -LeafBase parameter.

As we can observe, the file name ConvertString was returned without the ps1 extension. Here, the -LeafBase parameter was used above to get the filename without an extension.

Note: Only PowerShell 6.0 or later versions support this argument.

Using Get-Item Cmdlet

Use the Get-Item cmdlet to get filename from the path in PowerShell. Get-item cmdlet is used to get item at specified location.

Here, we used Get-Item cmdlet to get the filename from the specified path; we can see the file’s mode, last update date/time, length and name where we can find filename by looking under the Name column.

In our case, the ConvertString.ps1 represented the name of a file. If you only want to get the file name with its extension, append .Name at the end of the above command, as shown below.

We can see above now we only get the file name with the extension as ConvertString.ps1 from the given path. If you don’t want file extension, try this:

Now only the file name ConvertString is returned. Note .BaseName property above is used to get the file name, excluding its extension.

Using Get-ChildItem Cmdlet

Use the Get-ChildItem cmdlet to get the filename from given path.

The Get-ChildItem with the .Name property returned file names with extensions, and the .BaseName property returned file names without extensions.

Using GetFileName() Method

Use the GetFileName() method of the Path class to get the file name and extension of the given path.

The command [System.IO.Path]::GetFileName('C:\Intel\project\ConvertString.ps1') retrieved the file name from the specified file path C:\Intel\project\ConvertString.ps1 using the GetFileName() method of the System.IO.Path class.

Here, the GetFileName() method returned the file name and extension of the file but not the directory path. So, for example, if the file path is C:\Intel\project\ConvertString.ps1, the file name would be ConvertString.ps1.

If you want to retrieve the file name without the file extension, you can use the GetFileNameWithoutExtension() method of the System.IO.Path class instead. For example:

Getting Multiple File Names

In PowerShell, we can get multiple file names with or without extensions from the given path.

With Extensions

Use the Get-ChildItem cmdlet to retrieve items from one or multiple given locations pipes to the method GetFileName() to get the filenames with extension.

We used the Get-ChildItem cmdlet to extract items from one or more specified locations and piped to the method GetFileName() to get the filenames with extension. For example, the Get-ChildItem was used to get files from the given location C:\Intel\project\.

The -Filter parameter was used to get the files with the .s1 extension and sent the output to the next command using pipeline (|). Afterwards, we used the ForEach-Object to iterate over each file in the given directory.

Next, the -Process parameter was used with the GetFileName() method of the System.IO.Path class to extract the names of the files with extension. We can also get multiple files without an extension from the given directory. Let’s see below to understand how we can do this.

Without Extension

Use the Get-ChildItem cmdlet to extract items from one or more specified locations pipes to the method GetFileNameWithoutExtension() to get the filenames without extension.

You can observe that two files available at C:\Intel\project\ are returned using the above command. Note that the above code is the same as in the previous section; we have only replaced the GetFileName() method with the GetFileNameWithoutExtension() method to get filenames without their extensions.

That’s all about how to get filename from Path in PowerShell.

Was this post helpful?

Leave a Reply

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