Link to home
Start Free TrialLog in
Avatar of gd6627
gd6627Flag for United States of America

asked on

need to change user properties in AD

I need a powershell script to modify the following attributes in AD , this is what i have but it doesnt work if someone can see what i need to do . I have a csv file with samaccount and description fields only i need to do the rest but i need to know how to set -qaduser identity it hangs at this point


$users = 'c:\setqadtestfile.csv'

Import-Csv $users | `
foreach {
set-qaduser -Identity  $_.MySamAccount `
-Decription $_.Mydescription `
-Title `
-StreetAddress `
-Company  `
-Office  `
-Postalcode  `
-City  `
-StateOrProvince  `
-PhoneNumber  `
}
Avatar of GusGallows
GusGallows
Flag of United States of America image

You should be able to do this using a simple Set-User command with the ADSI extension. It would look something like this (assuming your headers in your CSV are MySamAccount,Mydescription):

$users = import-csv 'c:\setqadtestfile.csv'

foreach ($item in $users)
{
	$Id = $item.MySamAccount
	$desc = $item.Mydescription
	$usr = get-user $id
	$usrDN = $usr.DistinguishedName
	Set-User $id -Description $desc
	$AD = [ADSI]"LDAP://$usrDN"
	$AD."description" = $desc

Open in new window

ack, lose the Set-User line. I did not mean to keep that in there.
Let's try that again:

$users = import-csv 'c:\setqadtestfile.csv'

foreach ($item in $users)
{
	$Id = $item.MySamAccount
	$desc = $item.Mydescription
	$usr = get-user $id
	$usrDN = $usr.DistinguishedName
	$AD = [ADSI]"LDAP://$usrDN"
	$AD."description" = $desc
	$Ad.SetInfo()
}

Open in new window

If you are adding more than just the description attribute, make sure to use the attribute names as you see them in ADSI edit. For instance:
User Attribute Name                  ADSI attribute name
Title                                             title
StreetAddress                           streetAddress
Company                                   company
Office                                        physicalDeliveryOfficeName
Postalcode                                postalCode
City                                            l
StateOrProvince  `                    st
PhoneNumber                           telephoneNumber
Your header in your CSV would look like this:
MySamAccount,Mydescription,MyTitle,MyStreetAddress,MyCompany,MyOffice,MyPostalCode,MyCity,MyStateOrProvince,PhoneNumber

The code in the powershell script would looks as follows:
$users = import-csv 'c:\setqadtestfile.csv'

foreach ($item in $users)
{
	$Id = $item.MySamAccount
	$desc = $item.Mydescription
	$title = $item.MyTitle
	$address = $item.MyStreetAddress
	$company = $item.MyCompany
	$ofice = $item.MyOffice
	$zip = $item.MyPostalCode
	$city = $item.MyCity
	$state = $item.MyStateOrProvince
	$phone = $item.PhoneNumber
	$usr = get-user $id
	$usrDN = $usr.DistinguishedName
	$AD = [ADSI]"LDAP://$usrDN"
	$AD."description" = $desc
	$AD."title" = $title
	$AD."streetAddress" = $address
	$AD."company" = $company
	$AD."physicalDeliveryOfficeName" = $office
	$AD."postalCode" = $zip
	$AD."l" = $city
	$AD."st" = $state
	$AD."telephoneNumber" = $phone
	$Ad.SetInfo()
}

Open in new window

Avatar of gd6627

ASKER

Ok but am using cmdlets from quest  software in powergui can your script still work
ASKER CERTIFIED SOLUTION
Avatar of GusGallows
GusGallows
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gd6627

ASKER

Thank you so much it work fine