Comparing csv file to Folder file
Comparing csv file to Folder file
I am trying to compare a csv file with the following information:
User1,ID1
User2,ID2
User3,ID3
to a folder file with many folders that have the same names as the first column of the csv file.
So far I have this code but it is not working.
$input = Import-Csv 'C:Userscsvlist.csv' -Header "Users","ID"
$folder = Get-ChildItem H:Test | Sort-Object name
$compare = Compare-Object -ReferenceObject $input.Users -DifferenceObject $folder -IncludeEqual
if(($input.Users).SideIndicator -eq "=="){
echo "true"
}else{
echo "false"
}
Ultimately, my goal is to compare the headercolumn of the csv to the folders and see if it have the same name, if so, add new permission to the matched folder with the ID from the second headercolumn of the CSV. I understand it's a lot more than what I have in my code, but baby steps.
Test-Path
My logic was to compare the csv file to the folders to see if the user matches the folder name, then add new permission to the folder with the connected ID.
– Burainu
Jul 2 at 19:55
2 Answers
2
Why the compare? You would have to iterate the result. I'd iterate the csv and do a Test-Path with the user name.
$BaseDir = 'H:Test'
$input = Import-Csv 'C:Userscsvlist.csv' -Header "User","ID"
ForEach-Object ($Item in $input){
$UserPath = Join-Path $BaseDir $Item.User
If (Test-Path $UserPath -PathType Container){
"User {0} ID {1} Folder {2} exists" -f $Item.User,$UserPath,$Item.ID
} else {
# possibly create the folder
}
}
So, when you do Get-ChildItem
the result is an array of objects. Those are dotnet
objects with all their properties, methods etc available for you.
Get-ChildItem
dotnet
$folders = Get-ChildItem C:Windows -Directory
foreach($line in $csv) {
$folder = $folders | Where-Object {$_.Name -eq $line.Name}
if($folder -ne $null){
#ACL magic goes here, there are a number of articles on this
}
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Why the compare? You would have to iterate the result. I'd iterate the csv and do a
Test-Path
with the user name– LotPings
Jul 2 at 19:37