Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Powershell Get-ChildItem extract username from profile folder

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
--------

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading