PowerShell Get-ChildItem - 路径太长

1 powershell path

我一直在尝试格式化命令的输出表Get-ChildItem

我创建了一个脚本来扫描 中 定义的目录,$Source以搜索$Months和定义的文件$Size。这工作正常,但某些目录的长度超过 248 个字符,并且我收到文件路径太长错误。

所以我想$Source在输出到表中的目录列时不包含路径。

例如我想看到这个:

| file1.csv | Last Write Access | .\Desktop\Folder1 |  
| file2.txt | Last Write Access | .\Desktop\Folder2 |
Run Code Online (Sandbox Code Playgroud)

当 $Source 定义为C:\users\User1

这是我当前的代码:

| file1.csv | Last Write Access | .\Desktop\Folder1 |  
| file2.txt | Last Write Access | .\Desktop\Folder2 |
Run Code Online (Sandbox Code Playgroud)

Dri*_*104 7

还有另外 2 个解决方案

从 Windows 10 / Windows Server 2016(Build 1607 或更高版本)开始,您可以使用

Set-ItemProperty 'HKLM:\System\CurrentControlSet\Control\FileSystem' -Name 'LongPathsEnabled' -value 1
Run Code Online (Sandbox Code Playgroud)

如果您正在使用PowerShell 5.1或以上您可以使用

get-childitem -LiteralPath
Run Code Online (Sandbox Code Playgroud)

所以在这种情况下,您将定义$Source = "\\?\C:\Users\User1"并替换Get-ChildItem -Path "$Source\*.*" -Recurse -ForceGet-ChildItem -LiteralPath "$Source\*.*" -Recurse -Force