How to Get Details of a File in PowerShell?

Do you want to get meta data of a file using PowerShell? In this tutorial, I will explain how to get details of a file in PowerShell. We will cover various methods to retrieve file metadata, attributes, and content, etc.

To get details of a file in PowerShell, you can use the Get-ItemProperty cmdlet to retrieve properties such as file size, creation time, and last modified time. For example, Get-ItemProperty -Path “C:\MyFolder\File.txt” will list these properties for the specified file. To read the contents of a file, use Get-Content -Path “C:\MyFolder\File.txt”, and for bulk operations on multiple files, Get-ChildItem combined with Get-ItemProperty can provide detailed information for each file in a directory.

Get Details of a File using Get-ItemProperty

To get file details using PowerShell, you can use the Get-ItemProperty. This PowerShell cmdlet retrieves the properties of the item at a specified path, which can include a file or a folder. Let’s look at a basic example:

Get-ItemProperty -Path "C:\MyFolder\MyFile.txt"

This command will output properties such as the file’s LastWriteTime, Length (file size), and CreationTime, among others.

You can see the output in the screenshot below after I executed the above PowerShell script.

Get Details of a File in PowerShell

For a more detailed view, you can specify particular properties that you want to retrieve, like Name, Length, Lastwritetime, etc.:

Get-ItemProperty -Path "C:\MyFolder\MyFile.txt" -Name Length, LastWriteTime

This script will display only the size and last modified time of the specified file.

Get File Content using the Get-Content PowerShell cmdlet

If you want to read the contents of a file using PowerShell, you can use the Get-Content cmdlet. Here is the PowerShell command.

Get-Content -Path "C:\MyFolder\MyFile.txt"

This command will display the contents of “MyFile.txt” in the console. If you wish to read a certain number of lines from the file, you can use the -TotalCount parameter:

Get-Content -Path "C:\MyFolder\MyFile.txt" -TotalCount 5

This script will output the first five lines of the file.

Get File Attributes with Get-Item

To get more in-depth with file attributes, you can use the Get-Item PowerShell cmdlet. This cmdlet allows you to retrieve the file object and its attributes, such as ReadOnly, Hidden, and Archive. Here’s how you can use it:

$file = Get-Item -Path "C:\MyFolder\MyFile.txt"
$file.Attributes

The output will list all the attributes set on the file. You can also filter to check if a specific attribute is set:

$file = Get-Item -Path "C:\MyFolder\MyFile.txt"
$file.Attributes -match "ReadOnly"

If the file is read-only, this script will return True.

Use Get-ChildItem for Multiple Files

If you need to retrieve details for multiple files at once, then you can use the Get-ChildItem PowerShell cmdlet. This cmdlet can be used to list all files in a directory and then pipe the output to Get-ItemProperty for detailed information:

Get-ChildItem -Path "C:\MyFolder" -File | Get-ItemProperty | Select-Object Name, Length, LastWriteTime

This command will list the names, sizes, and last modified times for all files in the specified directory.

You can see the screenshot below; after I executed the above script, it displayed details of all the files.

Get Details of a File using Get-ItemProperty

Get File Details in CSV Format

Here is a complete script that retrieves specific details for all files in a directory and exports the information to a CSV file:

$files = Get-ChildItem -Path "C:\MyFolder" -File
$fileDetails = foreach ($file in $files) {
    $properties = Get-ItemProperty -Path $file.FullName
    [PSCustomObject]@{
        FileName = $properties.Name
        Size = $properties.Length
        LastModified = $properties.LastWriteTime
        IsReadOnly = ($properties.Attributes -match "ReadOnly")
    }
}

$fileDetails | Export-Csv -Path "C:\MyFolder\FileDetails.csv" -NoTypeInformation

This script first gathers all files in the specified directory, then iterates over each file, collecting its name, size, last modified time, and read-only status. The collected data is then exported to a CSV file.

Conclusion

In this PowerShell tutorial, I have explained how to get details of a file using Get-ItemProperty in PowerShell. Also, I have explained how to use Get-ChildItem PowerShell cmdlet to get file details of multiple files.

You may also like: