PowerShell script to update the userWorkstations attribute of Active Directory users in bulk from the information in a CSV file. Computer names can be added and/or removed from the attribute.
The userWorkstations attribute is a single-valued string. It is a comma delimited list of the NetBIOS names of the computers the user is allowed to logon to. If the attribute has no value, the user can logon to any computer in the domain. The Active Directory Users and Computers tool displays the value on the "Account" tab of the user properties. Click the "Log On To..." button to view or modify the computer names. The GUI parses the value to show the individual computer names on separate lines. The corresponding PowerShell parameter of the Set-ADUser cmdlet is LogonWorkstations.
The script requires a CSV file that specifies the users and the workstations to be added to and removed from the userWorkstations attribute. A header line should define the three fields: sAMAccountName, AddNames, and RemoveNames. If either AddNames or RemoveNames has more than one computer name, the names must be comma delimited and the entire value should be enclosed in double quotes. An example CSV file follows:
sAMAccountName,AddNames,RemoveNames
jwilson,minnesota,
fjohnson,,oregon
adanson,"utah,indiana","illinois,wisconsin"
dfgrant,,"wisconsin,colorado,wyoming"
In this example, user jwilson has one computer added and none removed. User fjohnson has no computers added and one removed. User adanson has two computers added and two removed. An example log file follows:
------------------------------------------------
UpdateUsersWorkstations.ps1
Version 1.0 - January 4, 2019
CSV file: c:\PowerShell\Example.csv
Log file: .\TestUserWS.log
Update users: False
Started: 1/4/2019 4:19:34 PM
------------------------------------------------
User: jwilson
Computers to add: minnesota
Computers to remove:
Existing value: oregon,wisconsin,utah
New value: oregon,wisconsin,utah,minnesota
User: fjohnson
Computers to add:
Computers to remove: oregon
Existing value: oregon
All values cleared
User: adanson
Computers to add: utah,indiana
Computers to remove: illinois,wisconsin
Existing value: utah,wisconsin,illinois
Computer indiana not found
New value: utah
User: dfgrant
Computers to add:
Computers to remove: wisconsin,colorado,wyoming
User not found!
------------------------------------------------
Finished: 1/4/2019 4:19:47 PM
------------------------------------------------
UpdateUserWorkstations.txt <<-- Click here to view or download the program