PowerShell 获取文件夹树 LastAccessed 中的所有文件并导出到 csv

问题描述 投票:0回答:1

我有一个 Powershell 脚本,用于尝试获取文件夹树中 LastAccessedTime > 365 天前的所有文件。我知道我的脚本正在使用 Plus 10 天 - 但这是为了测试目的,因为我刚刚创建了文件来测试它。

$Folder = 'D:\Temp\*.*'
$lastAccessDate = (Get-Date).AddDays(10)

Get-ChildItem $Folder -Recurse | Where-Object {
    $_.LastAccessTime -lt $lastAccessDate
} | Select-Object -ExpandProperty Name, LastAccessTime | 
 Export-CSV "C:\Temp\FileListing.csv" -NoTypeInformation -Encoding UTF8

我还根据此链接尝试了此代码学习PowerShell

Get-ChildItem $Folder -Recurse | Where-Object {
    $_.LastAccessTime -lt $lastAccessDate
} | Select-Object -ExpandProperty Expand - Property Name, LastAccessTime | 
 Export-CSV "C:\Temp\FileListing.csv" -NoTypeInformation -Encoding UTF8

第一个代码错误

Select-Object : Cannot convert 'System.Object[]' to the type 'System.String' required 
by parameter 'ExpandProperty'. Specified method is not supported.

此错误

Get-ChildItem $Folder -Recurse | Where-Object {
    $_.LastAccessTime -lt $lastAccessDate
} | Select-Object -ExpandProperty Name,LastAccessTime | 
 Export-CSV "C:\Temp\FileListing.csv" -NoTypeInformation -Encoding UTF8
Select-Object : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ExpandProperty'. Specified method is not supported.
At line:8 char:35
+ } | Select-Object -ExpandProperty Name,LastAccessTime |
+                                   ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Select-Object], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.SelectObjectCommand
powershell file properties-file
1个回答
0
投票

您只需从代码中删除

Expand
,如下所示:

$Folder = 'D:\Temp\*.*'
$lastAccessDate = (Get-Date).AddDays(10)

Get-ChildItem $Folder -Recurse | Where-Object {
    $_.LastAccessTime -lt $lastAccessDate
} | Select-Object -Property Name, LastAccessTime | 
 Export-CSV "C:\Temp\FileListing.csv" -NoTypeInformation -Encoding UTF8

第一个错误表示

Name
无法扩展,因为它是一个字符串,而不是包含属性的对象。
Select-Object -Property
选择沿管道传入的对象的给定属性,
Select-Object -ExpandProperty
选择沿管道传入的对象的给定对象的属性。

© www.soinside.com 2019 - 2024. All rights reserved.