【问题标题】:Find specific file types located within each user's %AppData% directory using powershell使用 powershell 查找位于每个用户的 %AppData% 目录中的特定文件类型
【发布时间】:2018-10-05 09:15:05
【问题描述】:

我正在尝试使用 powershell 脚本完成以下操作:

  • 查找位于每个用户的%AppData% 目录中的所有 json 文件。
  • 以下列格式输出到控制台:.json found in

我是 PowerShell 新手,目前有以下内容,

$AppData = Get-ChildItem $env:SystemDrive\Users | where {$_.PsIsContainer} | Foreach-Object {$_.FullName}
$Result=Get-ChildItem -Filter *.json -recurse

ForEach ($result in $appdata) { IF($Result -like '*.json') {$result }} 

我需要弄清楚如何将 appdata 的文件夹名称附加到 $AppData 变量输出。

有没有比使用 ForEach 循环更好的方法?

【问题讨论】:

  • Get-ChildItem 'C:\Users\*\AppData' -Filter '*.json' -Recurse

标签: powershell


【解决方案1】:

您可以使用ForEach-Object cmdlet 枚举元素,然后使用Write-Host cmdlet 在控制台中显示结果。像这样:

Get-ChildItem C:\TMP -Filter '*.json' -File -Recurse | ForEach-Object {
    Write-Host $('{0} found in {1}' -f $_.Name, ([System.IO.Path]::GetDirectoryName($_.FullName)))
}

【讨论】:

    猜你喜欢
    • 2023-01-21
    • 1970-01-01
    • 2015-01-07
    • 2011-03-26
    • 1970-01-01
    • 1970-01-01
    • 2011-08-21
    • 2015-01-26
    • 2018-04-22
    相关资源
    最近更新 更多