I am sure I have the variable name wrong, I have tried several variations and it just doesn’t seem to like me. Also, it is possible to put both the “remove-item” (different paths) under one “ForEach”?

ForEach blah {Remove-Item path1, Remove-Item path2}

Thanks in Advance. You guys rock!

$users = import-csv C:\Scripts\1.csv
Foreach ($samaccountname in $users){
Remove-Item '\\SHARE\CITRIXPROFILES\$_.samaccountname\Chrome\Default\Cache' -Recurse
}

ForEach ($samaccountname in (import-csv -path "C:\Scripts\1.csv"))
{
  Remove-Item '\\SHARE\CITRIXPROFILES\$_.samaccountname\Chrome\Default\Code Cache' -Recurse
}
4 Spice ups

My 1.csv looks like this:

samaccountname
Testa.Testa

Sorry about that.

powershell differentiates between douvle and single quote

you are using the wrong quotes

single → ( ’ ) → literal string
double → ( " ) → interpolates

But yeah you also use the wrong variable.
Is there a column in your CSV called ‘samaccountname’ ?

there are 2 foreach; foreach and foreach-object, you can not mix and match them as you did.

this should work, assuming there is a ‘samaccountname’ column in your csv

$users = import-csv "C:\Scripts\1.csv"
Foreach ($samaccountname in $users){
    Remove-Item "\\SHARE\CITRIXPROFILES\$($samaccountname.samaccountname)\Chrome\Default\Cache" -Recurse
    Remove-Item "\\SHARE\CITRIXPROFILES\$($samaccountname.samaccountname)\Chrome\Default\Code Cache" -Recurse
}

foreach:

foreach($item in $collection){
    $item
}

foreach-object:

$collection | foreach-object{
    $_ # or $PSITEM
}

for CSV I like to use it like so:

$csv = import-csv "file.csv"

foreach($row in $csv){
     $row.'columnNameHere'
}

it’s a bit more descriptive ¯_(ツ)_/¯

$csv = import-csv "C:\Scripts\1.csv"
Foreach ($row in $csv){
    Remove-Item "\\SHARE\CITRIXPROFILES\$($row.samaccountname)\Chrome\Default\Cache" -Recurse
    Remove-Item "\\SHARE\CITRIXPROFILES\$($row.samaccountname)\Chrome\Default\Code Cache" -Recurse
}

Thank you for the explanation of quotes. This works great.

See, I knew this crew rocked the casbah!

Many thanks