I have this script below which will pull an individual user’s account information and post it into a .txt file in my home directory for reference if the employee ever gets hired back to the company for the same position. My manager just gave me a list of 600 users this morning that I need to get the information from. I am at a loss as to how to import a csv file and have this script run on each user in the csv. Any help would be greatly appreciated.<\/p>\n
[CmdletBinding(SupportsShouldProcess=$True)]\nParam(\n\t[Parameter(Mandatory = $True)]\n\t[String]$UserName\n)\nImport-Module ActiveDirectory\n If ($UserName) {\n\t $UserName = $UserName.ToUpper().Trim()\n\t $Res = (Get-ADPrincipalGroupMembership $UserName | Measure-Object).Count\n\t If ($Res -GT 0) {\n\t\t Get-ADPrincipalGroupMembership $UserName | Select-Object -Property Name, GroupScope, GroupCategory | Sort-Object -Property Name | format-table -Autosize | out-file \"\\\\server\\network\\home\\($UserName).txt\"\n\t }\n }\n<\/code><\/pre>","upvoteCount":4,"answerCount":8,"datePublished":"2017-08-23T09:59:49.000Z","author":{"@type":"Person","name":"chrisrhode","url":"https://community.spiceworks.com/u/chrisrhode"},"acceptedAnswer":{"@type":"Answer","text":"
Advertisement
So, assuming the SamAccountName in the CSV you have is called UserName:<\/p>\n
Import-Csv c:\\path\\to\\your\\csv\\Users.csv | % { c:\\Path\\to\\your\\script\\Get-Groups.ps1 -Username $_.Username }\n<\/code><\/pre>","upvoteCount":2,"datePublished":"2017-08-23T10:20:52.000Z","url":"https://community.spiceworks.com/t/using-powershell-to-get-a-users-group-info/601604/2","author":{"@type":"Person","name":"martin9700","url":"https://community.spiceworks.com/u/martin9700"}},"suggestedAnswer":[{"@type":"Answer","text":"
Advertisement
I have this script below which will pull an individual user’s account information and post it into a .txt file in my home directory for reference if the employee ever gets hired back to the company for the same position. My manager just gave me a list of 600 users this morning that I need to get the information from. I am at a loss as to how to import a csv file and have this script run on each user in the csv. Any help would be greatly appreciated.<\/p>\n
[CmdletBinding(SupportsShouldProcess=$True)]\nParam(\n\t[Parameter(Mandatory = $True)]\n\t[String]$UserName\n)\nImport-Module ActiveDirectory\n If ($UserName) {\n\t $UserName = $UserName.ToUpper().Trim()\n\t $Res = (Get-ADPrincipalGroupMembership $UserName | Measure-Object).Count\n\t If ($Res -GT 0) {\n\t\t Get-ADPrincipalGroupMembership $UserName | Select-Object -Property Name, GroupScope, GroupCategory | Sort-Object -Property Name | format-table -Autosize | out-file \"\\\\server\\network\\home\\($UserName).txt\"\n\t }\n }\n<\/code><\/pre>","upvoteCount":4,"datePublished":"2017-08-23T09:59:49.000Z","url":"https://community.spiceworks.com/t/using-powershell-to-get-a-users-group-info/601604/1","author":{"@type":"Person","name":"chrisrhode","url":"https://community.spiceworks.com/u/chrisrhode"}},{"@type":"Answer","text":"Did you write this yourself?<\/p>\n
You need to to use import-csv and foreach-object here. Neally or Jiten will be along shortly to show the exact command but it should look like:<\/p>\n
import-csv \"path/to/file.csv\" | foreach-object {script}\n<\/code><\/pre>\n@alexw<\/a> @jitensh<\/a><\/p>","upvoteCount":1,"datePublished":"2017-08-23T10:24:23.000Z","url":"https://community.spiceworks.com/t/using-powershell-to-get-a-users-group-info/601604/3","author":{"@type":"Person","name":"jacobdurham","url":"https://community.spiceworks.com/u/jacobdurham"}},{"@type":"Answer","text":"Awesome! It’s working like a dream Martin9700! Thank you for your quick reply, this just saved me a few hours of typing.<\/p>\n