Powershell 将路径从FullName属性提取到结果中

Powershell 将路径从FullName属性提取到结果中,powershell,Powershell,我有这个剧本: Get-ChildItem -Path R:\MyFolder\archive -Recurse | >> Sort-Object -Property LastAccessTime | >> Select-object -Property Name, FullName 我希望路径没有文件名,而不是全名,因此我尝试如下调整: Get-ChildItem -Path R:\MyFolder\archive -Recurse | >> Sor

我有这个剧本:

Get-ChildItem -Path R:\MyFolder\archive -Recurse |
>>  Sort-Object -Property LastAccessTime |
>>  Select-object -Property Name, FullName
我希望路径没有文件名,而不是全名,因此我尝试如下调整:

Get-ChildItem -Path R:\MyFolder\archive -Recurse |
>>  Sort-Object -Property LastAccessTime |
>>  Select-object -Property Name, [System.IO.Path]::GetDirectoryName(fullname)
$rootPath = 'R:\MyFolder\archive'
Get-ChildItem -Path $rootPath -Recurse |
    Sort-Object -Property LastAccessTime |
    Select-Object -Property Name,
        @{ Name       = 'DirectoryName';
           Expression = { $_.FullName | Split-Path }}
显然,我对
PS
非常陌生-我是否清楚自己在尝试做什么,但失败了?

那么:

Get-ChildItem -Path 'R:\MyFolder\archive' -Recurse | Sort-Object -Property LastAccessTime | Select-object -Property Name, DirectoryName

通常,
Get-ChildItem
cmdlet返回不同类型的对象:

  • System.IO.FileInfo
  • System.IO.DirectoryInfo
不幸的是,在后者上找不到名为
DirectoryName
的属性。您可以使用
Get ChildItem-File
-File
参数消除此类对象),或使用计算属性,如下所示:

Get-ChildItem -Path R:\MyFolder\archive -Recurse |
>>  Sort-Object -Property LastAccessTime |
>>  Select-object -Property Name, [System.IO.Path]::GetDirectoryName(fullname)
$rootPath = 'R:\MyFolder\archive'
Get-ChildItem -Path $rootPath -Recurse |
    Sort-Object -Property LastAccessTime |
    Select-Object -Property Name,
        @{ Name       = 'DirectoryName';
           Expression = { $_.FullName | Split-Path }}
有关说明,请阅读:

-属性

指定要选择的属性。这些属性添加为 输出对象的NoteProperty成员。允许使用通配符

属性参数的值可以是新的计算值 财产要创建计算属性,请使用哈希表

有效密钥为:

  • 名称(或标签):
  • 表达或

您使用的不是
全名
,而是
目录