当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell 文件属性 - 获取文件属性

作者:精品下载站 日期:2024-12-14 16:45:21 浏览:14 分类:玩电脑

PowerShell 文件属性 - 获取文件属性


要在 PowerShell 中获取文件属性,您可以使用 Get-ChildItemGet-Item cmdlet。它返回指定文件上可用的文件属性或属性。

要获取所有可用属性的列表,请使用 Get-Member cmdlet。它从 Get-ChildItem 或 Get-Item 对象获取输入并返回文件属性。

在本文中,我们将讨论如何使用 Get-ChildItem 或 Get-Item cmdlet 在 PowerShell 中获取文件属性。

使用 Get-ChildItem 获取文件属性

在 PowerShell 中使用 Get-ChildItem cmdlet 获取文件项,默认情况下它会返回模式、LastWriteTime 和长度名称等基本属性。

Get-Item -Path D:\LogTest\FTP-02\get-log.py  

在上面的 PowerShell 脚本中,我们使用了 Get-ChildItem 命令,该命令将文件路径作为输入并返回文件属性。

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:\>  

使用Get-Member命令,您可以获得该文件的可用属性列表。

Get-Item -Path D:\LogTest\FTP-02\get-log.py | Get-Member -MemberType Properties

在上面的 PowerShell 脚本中,Get-ChildItem 获取文件并将文件对象作为输入传递给 Get-Member 命令。

Get-Member cmdlet 使用 MemeberType 参数获取文件的属性。

上述命令的输出返回属性,如下所示:

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);}

要获取所有文件属性,请使用带有 *(通配符)的 Properties 参数和 Force 参数。

Get-Item -Path D:\LogTest\FTP-02\get-log.py | Format-List -Property * -Force  

在上面的 PowerShell 脚本中,Get-ChildItem 从指定位置获取文件对象,并将其作为输入传递给 Format-List cmdlet。它使用 -Property * 来获取所有属性。

上述在 PowerShell 中检索文件属性的命令的输出是:

[玩转系统] PowerShell 文件属性 - 获取文件属性

在上面的输出中,File具有Archive文件属性,文件扩展名为py,以及文件创建时间、最后修改时间和目录等相关信息。

PowerShell 使用 Get-Item 获取文件属性

使用 PowerShell 中的 Get-Item cmdlet,您可以检索文件属性。

Get-Item -Path D:\LogTest\FTP-02\get-log.py | Format-List -Property * -Force 

在上面的 PowerShell 脚本中,Get-Item 命令获取文件项并将其传递给 Format-List cmdlet 以获取所有文件属性,如下所示。

[玩转系统] PowerShell 文件属性 - 获取文件属性

酷提示:如何使用 PowerShell 导出文件名中包含日期的 csv!

结论

我希望上述有关如何使用 PowerShell Get-ChildItem 和 Get-Item cmdlet 获取文件属性的文章对您有所帮助。

使用 Get-Member cmdlet 获取有关项目成员类型的信息。

您可以在 ShellGeek 主页上找到有关 PowerShell Active Directory 命令和 PowerShell 基础知识的更多主题。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯