0
votes

I'm trying to get the results of a Get-ChildItem to filter a list of these folders from a CSV

I have a number of homefolders that are named the same as the users SamAccountName (as is usual)

all the home folders are stored on my share: \DomainName\Users

My CSV contains the users which are names the same as the home folders Example:

SamAccountName
123456
123457
123458

The part of my script that I want to work goes like this:

$SAM = import-csv "C:\Scripts\HomeDrive.csv"
$HomeDriveSharePath = "\\Domain\users"
$HomeDirectories = Get-ChildItem $HomeDriveSharePath | Where {$_.Name -Match $SAM.samaccountname -AND $_.PsIsContainer -eq $true}

With my CSV this returns no results (although the folder for the example users in the CSV definitely exists)

If I run this, it returns all of the homefolders (as it should)

Get-ChildItem $HomeDriveSharePath

If I run this:

$HomeDirectories = Get-ChildItem $HomeDriveSharePath | Where {$_.Name -Match "123456" -AND $_.PsIsContainer -eq $true}

then I do get the corresponding folder displayed (proving the line kind of works and the folder does exist)

This needs to be run for hundreds of users with regards to a migration, Otherwise I'd just sit here doing it manually :-(

First time posting, apologies if this isn't formatted or explained quite right... a thorough search of this site and Google hasn't brought back the exact scenario I seem to have.

I'm quite stumped... your help appreciated,

Thanks

1
try replacing $SAM.samaccountname with $SAM.samaccountname.trim()Anthony Stringer
Hi Anthony. I tried as suggested and got this output: Get-ChildItem : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input. At line:1 char:42MithUK1

1 Answers

0
votes
$SAM = import-csv "C:\Scripts\HomeDrive.csv"
$HomeDriveSharePath = "\\Domain\users"
$HomeDirectories = Get-ChildItem $HomeDriveSharePath | Where {$_.PsIsContainer}

$HomeDirectories = $(foreach ($name in $sam) {
    $HomeDirectories | ? {$_.name -match $name.samaccountname.trim()}
})

or

$HomeDirectories = $sam | % { dir $HomeDriveSharePath -Filter $_.samaccountname.trim() -Directory -Name }