Powershell Get-ChildItem 从配置文件文件夹中提取用户名

发布于 2025-01-18 07:24:00 字数 555 浏览 1 评论 0原文

我有存储漫游用户配置文件的驱动器:

U:\Users\john.doe
U:\Users\john.wick
U:\Users\john.smith

我需要检查用户是否有存储在其配置文件中的 *.pdf扩展名的文件,

$a = Get-ChildItem "U:\users\" -Include *.pdf -Recurse | select FullName
foreach ($b in $a){

Write-Output $b



}

U:\Users\john.wick\desktop\file.pdf
U:\Users\john.wick\documents\a.pdf
U:\Users\john.doe\desktop\1.pdf
..................................

需要编写列以从路径中提取用户名,而一个带有完整文件路径的列。 怎么做?

john.doe
john.wick
--------

I have drive where roaming user profiles are stored:

U:\Users\john.doe
U:\Users\john.wick
U:\Users\john.smith

I need to check if users have files with *.pdf extensions stored in their profiles

$a = Get-ChildItem "U:\users\" -Include *.pdf -Recurse | select FullName
foreach ($b in $a){

Write-Output $b



}

Output

U:\Users\john.wick\desktop\file.pdf
U:\Users\john.wick\documents\a.pdf
U:\Users\john.doe\desktop\1.pdf
..................................

I need to write column to extract username from path, and one column with full file path.
How to do it ?

john.doe
john.wick
--------

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林散布 2025-01-25 07:24:00

调用 Get-ChildItem 不使用 -Recurse 来发现配置文件文件夹,然后使用 Where-Object + Get-ChildItem 查找包含 pdf 的文件夹:

$profilesWithPDFs = Get-ChildItem U:\Users\ -Directory |Where-Object { $_ |Get-ChildItem -File -Recurse -Filter *.pdf |Select -First 1 }

找到相关文件夹后,您只能获取名称:

$profilesWithPDFs |ForEach-Object -MemberName Name

Call Get-ChildItem without -Recurse to discover the profile folders, then use Where-Object + Get-ChildItem to find the ones containing pdfs:

$profilesWithPDFs = Get-ChildItem U:\Users\ -Directory |Where-Object { $_ |Get-ChildItem -File -Recurse -Filter *.pdf |Select -First 1 }

Once you've discovered the relevant folders, you can grab the name only:

$profilesWithPDFs |ForEach-Object -MemberName Name
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文