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”?<\/p>\n
ForEach blah {Remove-Item path1, Remove-Item path2}<\/p>\n
Thanks in Advance. You guys rock!<\/p>\n
$users = import-csv C:\\Scripts\\1.csv\nForeach ($samaccountname in $users){\nRemove-Item '\\\\SHARE\\CITRIXPROFILES\\$_.samaccountname\\Chrome\\Default\\Cache' -Recurse\n}\n\nForEach ($samaccountname in (import-csv -path \"C:\\Scripts\\1.csv\"))\n{\n Remove-Item '\\\\SHARE\\CITRIXPROFILES\\$_.samaccountname\\Chrome\\Default\\Code Cache' -Recurse\n}\n<\/code><\/pre>","upvoteCount":4,"answerCount":6,"datePublished":"2021-06-02T17:08:04.000Z","author":{"@type":"Person","name":"mncowboy","url":"https://community.spiceworks.com/u/mncowboy"},"acceptedAnswer":{"@type":"Answer","text":"powershell differentiates between douvle and single quote<\/p>\n
you are using the wrong quotes<\/p>\n
single → ( ’ ) → literal string
\ndouble → ( \" ) → interpolates<\/p>\n
But yeah you also use the wrong variable.
\nIs there a column in your CSV called ‘samaccountname’ ?<\/p>\n
there are 2 foreach; foreach and foreach-object, you can not mix and match them as you did.<\/p>\n
this should work, assuming there is a ‘samaccountname’ column in your csv<\/p>\n
$users = import-csv \"C:\\Scripts\\1.csv\"\nForeach ($samaccountname in $users){\n Remove-Item \"\\\\SHARE\\CITRIXPROFILES\\$($samaccountname.samaccountname)\\Chrome\\Default\\Cache\" -Recurse\n Remove-Item \"\\\\SHARE\\CITRIXPROFILES\\$($samaccountname.samaccountname)\\Chrome\\Default\\Code Cache\" -Recurse\n}\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2021-06-02T17:11:53.000Z","url":"https://community.spiceworks.com/t/remove-item-import-csv/801614/3","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},"suggestedAnswer":[{"@type":"Answer","text":"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”?<\/p>\n
ForEach blah {Remove-Item path1, Remove-Item path2}<\/p>\n
Thanks in Advance. You guys rock!<\/p>\n
$users = import-csv C:\\Scripts\\1.csv\nForeach ($samaccountname in $users){\nRemove-Item '\\\\SHARE\\CITRIXPROFILES\\$_.samaccountname\\Chrome\\Default\\Cache' -Recurse\n}\n\nForEach ($samaccountname in (import-csv -path \"C:\\Scripts\\1.csv\"))\n{\n Remove-Item '\\\\SHARE\\CITRIXPROFILES\\$_.samaccountname\\Chrome\\Default\\Code Cache' -Recurse\n}\n<\/code><\/pre>","upvoteCount":4,"datePublished":"2021-06-02T17:08:04.000Z","url":"https://community.spiceworks.com/t/remove-item-import-csv/801614/1","author":{"@type":"Person","name":"mncowboy","url":"https://community.spiceworks.com/u/mncowboy"}},{"@type":"Answer","text":"My 1.csv looks like this:<\/p>\n
\n\n\n\nsamaccountname<\/th>\n<\/tr>\n<\/thead>\n \n\nTesta.Testa<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>Sorry about that.<\/p>","upvoteCount":0,"datePublished":"2021-06-02T17:10:30.000Z","url":"https://community.spiceworks.com/t/remove-item-import-csv/801614/2","author":{"@type":"Person","name":"mncowboy","url":"https://community.spiceworks.com/u/mncowboy"}},{"@type":"Answer","text":"
foreach:<\/p>\n
foreach($item in $collection){\n $item\n}\n<\/code><\/pre>\nforeach-object:<\/p>\n
$collection | foreach-object{\n $_ # or $PSITEM\n}\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2021-06-02T17:16:16.000Z","url":"https://community.spiceworks.com/t/remove-item-import-csv/801614/4","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"for CSV I like to use it like so:<\/p>\n
$csv = import-csv \"file.csv\"\n\nforeach($row in $csv){\n $row.'columnNameHere'\n}\n<\/code><\/pre>\nit’s a bit more descriptive ¯_(ツ)_/¯<\/p>\n
$csv = import-csv \"C:\\Scripts\\1.csv\"\nForeach ($row in $csv){\n Remove-Item \"\\\\SHARE\\CITRIXPROFILES\\$($row.samaccountname)\\Chrome\\Default\\Cache\" -Recurse\n Remove-Item \"\\\\SHARE\\CITRIXPROFILES\\$($row.samaccountname)\\Chrome\\Default\\Code Cache\" -Recurse\n}\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2021-06-02T17:17:36.000Z","url":"https://community.spiceworks.com/t/remove-item-import-csv/801614/5","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"Thank you for the explanation of quotes. This works great.<\/p>\n
See, I knew this crew rocked the casbah!<\/p>\n
Many thanks<\/p>","upvoteCount":0,"datePublished":"2021-06-02T18:27:09.000Z","url":"https://community.spiceworks.com/t/remove-item-import-csv/801614/6","author":{"@type":"Person","name":"mncowboy","url":"https://community.spiceworks.com/u/mncowboy"}}]}}