To get file attributes in PowerShell, you can use Get-ChildItem
or Get-Item
cmdlets. It returns the file attributes or properties available on the specified files.
To get the list of all properties available, use the Get-Member cmdlet. It takes the input from the Get-ChildItem or Get-Item objects and returns the file properties.
In this article, we will discuss how to get file attributes in PowerShell using the Get-ChildItem or Get-Item cmdlet.
Get File Attributes using Get-ChildItem
Use the Get-ChildItem cmdlet in PowerShell to get file items, by default it returns the basic properties like mode, LastWriteTime, and Length Name.
Get-Item -Path D:\LogTest\FTP-02\get-log.py
In the above PowerShell script, we have used the Get-ChildItem command which takes the file path as input and returns the file attributes.
PS D:\> Get-Item -Path D:\LogTest\FTP-02\get-log.py
Directory: D:\LogTest\FTP-02
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 02-09-2021 11:32 1338 get-log.py
PS D:\>
Using the Get-Member command, you can get the list of properties available for the file.
Get-Item -Path D:\LogTest\FTP-02\get-log.py | Get-Member -MemberType Properties
In the above PowerShell script, Get-ChildItem
gets the file and passes the file object as input to the Get-Member command.
Get-Member cmdlet uses the MemeberType
parameter to get properties on the file.
The output of the above command returns the Properties as given below:
PS D:\> Get-Item -Path D:\LogTest\FTP-02\get-log.py | Get-Member -MemberType Properties
TypeName: System.IO.FileInfo
Name MemberType Definition
---- ---------- ----------
LinkType CodeProperty System.String LinkType{get=GetLinkType;}
Mode CodeProperty System.String Mode{get=Mode;}
Target CodeProperty System.Collections.Generic.IEnumerable`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] ...
PSChildName NoteProperty string PSChildName=get-log.py
PSDrive NoteProperty PSDriveInfo PSDrive=D
PSIsContainer NoteProperty bool PSIsContainer=False
PSParentPath NoteProperty string PSParentPath=Microsoft.PowerShell.Core\FileSystem::D:\LogTest\FTP-02
PSPath NoteProperty string PSPath=Microsoft.PowerShell.Core\FileSystem::D:\LogTest\FTP-02\get-log.py
PSProvider NoteProperty ProviderInfo PSProvider=Microsoft.PowerShell.Core\FileSystem
Attributes Property System.IO.FileAttributes Attributes {get;set;}
CreationTime Property datetime CreationTime {get;set;}
CreationTimeUtc Property datetime CreationTimeUtc {get;set;}
Directory Property System.IO.DirectoryInfo Directory {get;}
DirectoryName Property string DirectoryName {get;}
Exists Property bool Exists {get;}
Extension Property string Extension {get;}
FullName Property string FullName {get;}
IsReadOnly Property bool IsReadOnly {get;set;}
LastAccessTime Property datetime LastAccessTime {get;set;}
LastAccessTimeUtc Property datetime LastAccessTimeUtc {get;set;}
LastWriteTime Property datetime LastWriteTime {get;set;}
LastWriteTimeUtc Property datetime LastWriteTimeUtc {get;set;}
Length Property long Length {get;}
Name Property string Name {get;}
BaseName ScriptProperty System.Object BaseName {get=if ($this.Extension.Length -gt 0){$this.Name.Remove($this.Name.Length - $this.Extension.Length)}else{$this....
VersionInfo ScriptProperty System.Object VersionInfo {get=[System.Diagnostics.FileVersionInfo]::GetVersionInfo($this.FullName);}
To get all file attributes, use the Properties parameter with * (wildcard) and Force parameter.
Get-Item -Path D:\LogTest\FTP-02\get-log.py | Format-List -Property * -Force
In the above PowerShell script, Get-ChildItem gets the file object from the specified location and passes it as input to the Format-List cmdlet. It uses -Property * to get all properties.
The output of the above command to retrieve file attributes in PowerShell is:
In the above output, the File has Archive file attributes, the file extension is py, and other relevant information about file creation time, last modified, and directory.
PowerShell Get File Attributes using Get-Item
Using the Get-Item cmdlet in PowerShell, you can retrieve file attributes.
Get-Item -Path D:\LogTest\FTP-02\get-log.py | Format-List -Property * -Force
In the above PowerShell script, the Get-Item command gets the file item and passes it to the Format-List
cmdlet to get all file properties as given below.
Cool Tip: How to export csv with date in file name using PowerShell!
Conclusion
I hope the above article on how to get file attributes using PowerShell Get-ChildItem and Get-Item cmdlets is helpful to you.
Use the Get-Member cmdlet to get information about member types on the item.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.