Set-ADuser -Identity extensionattribute not working - powershell

I can't get this to work, don't understand it because it works on a single user. Is it not possible to use a variable with the cmdlet Set-ADuser ?
I want to change extensionattributes from a csv or textfile.
This is my script:
Import-Module ActiveDirectory
$users = Import-Csv C:\csv.csv
Set-ADUser -Server servername -Identity $users -Replace #{extensionAttribute12="TEST"}
I am getting this error:
Set-ADUser : Cannot convert 'System.Object[]' to the type
'Microsoft.ActiveDirectory.Management.ADUser' required by parameter
'Identity'. Specified method is not supported.

users is an array.
Try below code:
Import-Module ActiveDirectory
$users = Import-Csv C:\csv.csv
foreach($user in $users)
{
Set-ADUser -Server servername -Identity $user -Replace #{extensionAttribute12="TEST"}
}

Import-Csv returns an array of your csv rows and therefore, Set-ADUser needs to be used within a loop of those rows.
$users = Import-Csv C:\csv.csv
foreach($user in $users) {
Set-ADUser -Server servername -Identity $user -Replace #{extensionAttribute12="TEST"}
}

Related

Set-ADuser in Foreach

I tried to set the value Company into the ad field Company and it should Filter by Homepage (this works). And I get this error:
cmdlet Set-ADUser at command pipeline position 1
Supply values for the following parameters:
Identity:
$users = $null
$users = Get-ADUser -Filter 'Homepage -like "www.test.ch"' -Searchbase “OU=TestOu, OU=test Lyss, DC=ads,DC=test,DC=CH"
ForEach($user in $users)
{
Set-ADUser -Identity -Company 'Company'
}
Right, in your foreach loop, you're calling the -Identity parameter but never giving any value to the argument, like -Identity $user. In addition, it can be piped directly:
Get-ADUser -Filter "Homepage -like 'www.test.ch'" -Searchbase 'OU=TestOu,OU=test Lyss,DC=ads,DC=test,DC=CH' |
Set-ADUser -Company 'Company'

I need to delete users from Active Directory using a imported csv file

I have a CSV file with a list of user names, I need to delete all of these users from Active Directory using the Remove-ADObject command. I am not very familiar with the syntax for this command - hoping you guys can help me here.
Import-Module activedirectory
$list = Import-CSV C:\Users\user\Desktop\deleteuserstest.csv
forEach ($item in $list) {
$samAccountName = $item.samAccountName
Remove-ADobject -Identity $samAccountName
}
You have to use DN or GUID with Remove-ADObject. You can do something like this:
Import-Module ActiveDirectory
$list = Import-CSV C:\Users\user\Desktop\deleteuserstest.csv
forEach ($item in $list) {
$samAccountName = $item.samAccountName
#Get DistinguishedName from SamAccountName
$DN = Get-ADuser -Identity $Samaccountname -Properties DistinguishedName |
Select-Object -ExpandProperty DistinguishedName
#Remove object using DN
Remove-ADObject -Identity $DN
}

Get-ADPrincipalGroupMembership : Cannot validate argument on parameter 'Identity'. When i try to delete memberof properties from disabled users

We have a lot of disabled users, I want to write a script to delete the memberof property and keep the default (domain user). The .csv file I'm importing has a list of 5 samaccountname for testing purpose. when I execute this script I get this error message.
I do not get this message when I run the script for individual users but when I import the .csv file with the list of users I receive this error. Thanks for the help in advance.
c:\user\..\Desktop> .\powerAD.ps1
Get-ADPrincipalGroupMembership : Cannot validate argument on parameter 'Identity'. The
argument is null or empty. Provide an argument that is not null or empty, and then try
the command again.
This is the script I wrote:
This there something wrong with my syntax??
Import-Module ActiveDirectory
ForEach ($user in (import-csv -path "C:\users\j\desktop\ADUSER1.csv"))
{
Get-ADPrincipalGroupMembership -Identity $user.samaccountname |
% {Remove-ADPrincipalGroupMembership -Identity $user.samaccountname -MemberOf -confirm:$false $_}
}
.csv file is in this format.
jbry
pbarb
dvan
Screenshot from excel
The issue is your csv file. You are calling for the samaccountname property from it, but no column has that as the column header. Either read it in as a text file with Get-Content or give it a header.
ForEach ($user in (Get-Content "C:\users\j\desktop\ADUSER1.csv")) {
Get-ADPrincipalGroupMembership -Identity $user |
? {$_.Name -ne "Domain Users"} |
% {Remove-ADPrincipalGroupMembership -Identity $user -MemberOf $_}
}
Based on comments above and your linked image from Excel, BenH is probably correct. What you need to do now is convert your names, which are strings in PowerShell, into ADUser objects to pass as the -Identity parameter to Get-ADPrincipalGroupMembership and Remove-ADPrincipalGroupMembership:
foreach ($user in (Import-CSV -Path "C:\users\j\desktop\ADUSER1.csv"))
{
$u = Get-ADUser -Filter 'sAMAccountName -eq $user'
$u | Get-ADPrincipalGroupMembership |
Where-Object {$_.name -ne "Domain Users"} |
ForEach-Object {Remove-ADPrincipalGroupMembership -Identity $u -MemberOf $_}
}

Powershell-Update attribute "departmentNumber"

I have the following script - it works except for the departmentNumber. Any ideas?
# Import AD Module
Import-Module ActiveDirectory
# Import CSV into variable $users
$users = Import-Csv -Path C:\users.csv
# Loop through CSV file and update users if they exist
foreach ($user in $users) {
Get-ADUser -Filter "SamAccountName -eq '$($user.samaccountname)'" - Properties * |
Set-ADUser -EmailAddress $($user.EmailAddress) -Title $($user.Title) -Office $($user.Office) -OfficePhone $($user.OfficePhone) -departmentNumber $($user.departmentNumber) }
EDIT:
I tried the following using the "-Add" operator:
Import-Module ActiveDirectory
$users = Import-Csv -Path C:\users.csv
foreach ($user in $users) {
Get-ADUser -Filter "SamAccountName -eq '$($user.samaccountname)'" -Properties * |
Set-ADUser -Add #{departmentNumber = "$($user.departmentNumber)"}
}
And the following, using the "-Replace" operator:
Import-Module ActiveDirectory
$users = Import-Csv -Path C:\users.csv
foreach ($user in $users) {
Get-ADUser -Filter "SamAccountName -eq '$($user.samaccountname)'" -Properties * |
Set-ADUser -Replace #{departmentNumber = "$($user.departmentNumber)"}
}
Still no luck - do I have the syntax messed up?
I'm taking a quick look at the help for Set-ADUser: https://technet.microsoft.com/en-us/library/ee617215.aspx
I see a parameter for -Department, but not one for -DepartmentNumber
Will it work within your company's structure to just use -Department ?
For a custom attribute, you should be able to use the -Add parameter
-Add #{otherTelephone='555-222-1111', '555-222-3333'; otherMobile='555-222-9999' }
In order to use -Add or -Replace, you'll need to use a hashtable as your input instead of a string.
Here's a quick set of commands I used to convert a string into a single item hashtable:
$DeptNo = 'test'
$hash = #{}
$hash.Add('DepartmentNumber',$DeptNo)
$hash
Output from $hash:
Name Value
---- -----
DepartmentNumber test
I think you'll then be able to use -Add $hash to get it to do what you want.
Here is my code that got it working:
Set-ADUser -Identity $samName -Add #{'departmentNumber'="$UserDept"}
It is important to note you can only use -Add if nothing is there
otherwise use -Replace
$UserDept was a number but the quotes make it a string
and the attribute must have single quotes around it.

Update Active Directory "mail" attribute via PowerShell

I'm trying to update the email address listed in AD for all the users in a particular OU. This is the powershell script I'm using, but it's not working properly
Import-Module ActiveDirectory
Get-ADUser -Filter * -SearchBase "OU=OtherOU,OU=SomeOu,DC=Domain,DC=local" | Set-ADUser -email $_.samaccountname#domain.com
I think it's because $_.samaccountname isn't returning anything when I try to do Set-ADUser.
Can anyone point me in the right direction for fixing this? Thanks!
Create a csv file with SamAccountName & email address
"SamAccountName","EmailAddress"
"john","john#xyz.com"
step 1: import to a variable
$users = Import-Csv .\email.csv
step 2: Call the variable
foreach ($user in $users) {
Set-ADUser -Identity $user.SamAccountName -EmailAddress $user.EmailAddress
}
In the current context $_ is null. You need to use Foreach-Object in order for $_ to be available.
Get-ADUser -Filter * ... | Foreach-Object{
Set-ADUser -Identity $_ -Email "$($_.samaccountname)#domain.com"
}
I suspect you'll need to use a subexpression for that:
"$($_.samaccountname)#domain.com"
Assuming username is domain\user1 or user1#domain.com
$user = "user1"
Set-ADUser $user -emailaddress "firtname.lastname#xyz.com"
Get-ADUser -Identity $user -Properties emailaddress
Get-ADUser -Filter * -SearchScope Subtree -SearchBase "OU=OUName,DC=domain,DC=com" |
Foreach-Object { Set-ADUser -Identity $_ -Email "$($_.samaccountname)#domain.com" }
This is from:
https://social.technet.microsoft.com/wiki/contents/articles/33311.powershell-update-mail-and-mailnickname-for-all-users-in-ou.aspx