The Set-ADUser is a very powerful cmdlet that allows for changing a users attributes in Active Directory. In this article, the Set-ADUser will be used to demonstrate how to change the logon script portion of a Users account in Active Directory.
First, lets look at our User Account in question.
The logon script is blank. There are 3 user accounts for this demo that we are going to change: ProfileDemo001, ProfileDemo002, ProfileDemo003.
In order to run the Set-ADUser command, we need to prepare some prep work. First, a CSV file needs to be created with the usernames that are going to be changed. Below is a screenshot showing the CSV file inside Excel.
It is now time to fire up PowerShell with Active Directory Modules.
The command is the following : Import-CSV ProfileDemo.csv | ForEach-Object {Set-ADUser $_.Name -ScriptPath “\\logonScripts\Demo.vbs”}
Explanation of the Script : The Import-CSV section allows us to take a list of usernames, and feed (aka PIPE) them into the cmdlet “ForEach-Object”. The ForEach-Object is required since the Set-ADUser does not accept piped input. The ForEach-Object will go through all the names in the excel sheet, and set the parameters as needed. A full list of the parameter options can be found at : http://technet.microsoft.com/en-us/library/ee617215.aspx Once the script is done, we are left with the C:\PS> prompt.
To double-check, a quick glance at some of the users will show that the command has completed successfully.
Summary : The Set-ADUser cmdlet is a major time saver when combined with the Import-CSV and ForEach-object cmdlets. Over time, I have noticed that I consistently use the following statement very often “Import-CSV list.csv | ForEach-object { X }” where X is the cmdlet that does not accept a pipe input. Please leave questions in the comments if there are any problems.
Brilliant!
i want to update logon script for about 100 users for each user i have the samaccount name and same as a file for each users , means 100 users and 100 files placed in netlogon with samaccount1.bat,samaccount1.bat etc
can u guide me how i can do that ?
just to update my files is same as samaccoutn name like username.bat , for each user each file