Can someone please tell me how I can rename sn and givenName of a contact object in active directory?
This is how I query the data in my contacts OU:
Get-ADObject -Filter {(ObjectClass -eq "contact") -and (givenName -like "*myName*") } -Properties sn, givenName -SearchBase "OU=contacts,DC=domain,DC=name"
From Microsoft:
To modify the given name, surname and other name of a user, use the
Set-ADUser cmdlet
My goal is to modify the values of both properties because I have text that is all in lowercase and I was hoping to use something like:
$TextInfo = (Get-Culture).TextInfo
$TextInfo.ToTitleCase("one-two three")
Desired Output: One-Two Three
to get a capital letter for each word.
Some examples would be great.
If you know of a GUI-Tool that does what I need then please share as well.
To change the attributes on a contact AD object you will need to use the Set-ADOject Cmdlet, with the -Replace parameter. Set-ADUser will only set attributes of an AD object of type user.
The following code will take the full name of the contact object, search for it in AD and then change the GivenName, SN and DisplayName to title case.
I have added the DisplayName as this field is not automatically update when you change the first and last name, but you can delete this part if it's not needed.
$fullname = "My Name"
$contact = Get-ADObject -Filter {(ObjectClass -eq "contact") -and (Name -like $fullname)} -Properties * -SearchBase "OU=contacts,DC=domain,DC=name"
$TextInfo = (Get-Culture).TextInfo
$GivenNametoTitleCase = $TextInfo.ToTitleCase(($contact.givenName).ToLower())
$SNtoTitleCase = $TextInfo.ToTitleCase(($contact.sn).ToLower())
$DisplayNametoTitleCase = $TextInfo.ToTitleCase(($contact.DisplayName).ToLower())
$contact | Set-ADObject -Replace #{GivenName=$GivenNametoTitleCase;`
sn=$SNtoTitleCase;`
DisplayName=$DisplayNametoTitleCase;}
Related
I have a list of displaynames and I need to get their AD informations.
Get-Content "C:\displaynames.txt" |
foreach {
$givenname,$surname = $_ -split ' '
if (Get-ADUser -Filter "surname -eq '$surname' -and givenname -eq '$givenname'"){
Get-ADUser -Filter { displayName -match $_} -Properties EmailAddress, Manager | Select Givenname, Surname, SamAccountName, EmailAddress, Manager}
else {Get-ADUser -Filter { displayName -like "AD Test"} -Properties EmailAddress, Manager | Select Givenname, Surname, SamAccountName, EmailAddress, Manager}
} | Export-Csv -Path C:\result.csv
This works fine, but only if users have no middle names ex. John Moore
If the user has a middle name, it doesn't pick it up.
How can I change the script so it picks up users with middle names ex. John Roger Moore?
As Mathias R. Jessen already commented, you can use the -Filter on property DisplayName directly.
The Filter should be a string, not a scriptblock.
Using -Filter also has the advantage that you can suppress exceptions being thrown, so I would build in a step to confirm that we indeed did find a user with that displayname:
Get-Content "C:\displaynames.txt" | ForEach-Object {
$user = Get-ADUSer -Filter "DisplayName -eq '$_'" -Properties DisplayName, EmailAddress, Manager -ErrorAction SilentlyContinue
if ($user) {
# output the wanted properties as **object**
$user | Select-Object Givenname, Surname, SamAccountName, EmailAddress, Manager
}
else {
# nobody in this domain with a displayname like that..
Write-Warning "User '$_' could not be found.."
}
} | Export-Csv -Path 'C:\result.csv' -NoTypeInformation
Note that the Manager property is in the form of the managers DistinguishedName. If you want to get other properties for the manager, like his/her name, you will have to use Get-ADUser -Identity $user.Manager to get the wanted property there too
The basic question here is how to account for middle names.
PowerShell 5 has some AI-powered cmdlets.
Here, I will quote an example from the documentation.
Example 2: Simplify format of a string
$composers = #("Johann Sebastian Bach", "Wolfgang Amadeus Mozart", "Frederic Francois Chopin", "Johannes Brahms")
$composers | Convert-String -Example "first middle last=last, first"
Bach, Johann
Mozart, Wolfgang
Chopin, Frederic
Brahms, Johannes
The first command creates an array that contains first, middle and last names. Note that the last entry has no middle name.
The second command formats the names according to the example. It puts the last name first in the output, followed by the first name. All middle names removed; entry without middle name is handled correctly.
Convert-String (Microsoft.PowerShell.Utility) - PowerShell | Microsoft Docs
I m trying to use a PowerShell script to ask for name and last name and then display the samAccountname I tried this and it did not work.
$Names = Read-host "Jack Robinson"
$Usernames =
Get-ADUser -Filter "FirstName -eq $($_.FirstName) -and Surname -eq $($_.LastName)" -Properties 'SamAccountName' |
Select-Object -ExpandProperty 'SamAccountName'
In terms of taking input from a csv see #wasif Hasan's helpful answer. In terms of your particular code with no csv input - you are better off separating your request for first name and surname with two read-host requests. Take note of the attribute names GivenName for firstname and Surname for Last name
$firstname = read-host "first name"
$LastName = read-host "LastName"
Get-ADUser -Filter "GivenName -eq '$FirstName' -and Surname -eq '$LastName'"|
Select-Object -ExpandProperty 'SamAccountName'
Alternatively if you are unable to double the read-host requests as shown above you can attempt to filter on displayname which usually consists of firstname and lastname
$name = read-host "name"
Get-ADUser -Filter "Displayname -eq '$Name'"|
Select-Object -ExpandProperty 'SamAccountName'
Not sure why you used the pipeline variable when you are not taking input from anywhere. Also in AD user object Firstname is called GivenName and Lastname is called Surname. If you are taking input from csv like this:
FirstName,LastName,
xxx,xxx,
...
then try this:
$usernames = Import-Csv "filepath.csv" -Header GivenName,Surname -Delimiter "," | ForEach {
Get-ADUser -Filter {GivenName -like $_.GivenName -and Surname -like $_.Surname} | Select-Object -ExpandProperty sAMaccountName
}
I assume "Jack Robinson" is supposed to be a sample input. As you have it, it is the prompt that Read-Host will show the user. You probably want something like this:
$Names = Read-Host "Enter the name"
Separating the first and last names based on spaces is never foolproof, especially in cases of two or three-word last names. So you should either separate the prompt into first and last, like Itchydon's answer shows, or you can use ambiguous name resolution (ANR), which would let you search by the full name. That would look something like this:
$Names = Read-Host "Enter the name"
$Usernames =
Get-ADUser -LDAPFilter "(anr=$Names)" -Properties 'SamAccountName' |
Select-Object -ExpandProperty 'SamAccountName'
Searching by first and last name is never an exact science, since you could have multiple people in your organization by the same name, but ANR does make that a little worse because it does a "contains" kind of search. So if you search for my name, "Gabriel Luci", for example, you would also find "Gabriele Luciano".
It just depends on your use. If your user will be picking the right account, then ANR is an easy way to find accounts. But if this is part of some automation where you want to match names exactly, then you will have to split up the first and last names at the input and search for exact matches.
I try to add every User of an AD with a special description to a group wich contains the Department Attribute (up to 3 digits) as a suffix.
For Example
A User "Sam Test" has the Description "Boss" and the Department "123".
He should be added to Testgroup_123.
My Goal
Write a Script to add the Users to their associated Testgroup_???.
There can only be one Boss(User) in a Testgroup_???.
For testing reasons I only try to output the name.
This is my Code so far:
import-module ActiveDirectory
$user =
Get-ADUser -filter {(description -like "Boss") -or
(description -like "boss") -or
(description -like "Assistant")} -searchbase "OU=TestOU,DC=TE,DC=ADS" -Properties Enabled, description, sAMAccountName, Department | select Department | Foreach {Write-Host "Testgroup_$user<-empty?"}
If I understand your right, try this code:
$Users = Get-ADUser -Filter * -Properties Description,Department
foreach ($user in $Users)
{
if ($user.Description -match "Boss|Assistant")
{
$Dep = $User.Department
if (-not(Get-ADGroup "Testgroup_$Dep"))
{
New-ADGroup -Path "OU=TestOU,DC=TE,DC=ADS" -Name "Testgroup_$Dep" -GroupScope Global
}
else
{
$GroupMembers = Get-ADGroupMember -Identity "Testgroup_$Dep" | Select -ExpandProperty SamAccountName
if ($User.SamAccountName -notin $GroupMembers)
{
Add-ADGroupMember -Identity "Testgroup_$Dep" -Members $User
}
}
}
}
First it gets all the users
Check for each user for description match of "Boss" or "Assistant"
Get the department attribute for the user (just for example 666)
Check if Group name "Testgroup_666" Exist, if not Create new one in the path you defined
Check if the user is not already a member of this group, if not add add the user to the group
I am trying to figure out how to grab a userID's last name attribute but not sure how. I have tried to look at some examples but are falling short. Below is what I have so far but want to either get the last name attribute separately or grab the display name (which is first.last) and pull all the information after the period. Please help
Import-Module activedirectory
#$userID = Get-Aduser -filter *
$userIDs = Get-Aduser "w35522"
foreach ($lastName in $userIDs) {
$lastname = (get-Aduser -Filter * -Properties displayname).lastname
}
write-host $lastName
Update:
Thanks to comment below I changed lastname to surname. Only issue is now it prints the OU too which I don't want.
Import-Module activedirectory
#$userID = Get-Aduser -filter *
$userIDs = Get-Aduser "w35522"
foreach ($user in $userIDs) {
$lastname = get-Aduser -Identity $userIDs -Properties * | Select-Object name, GivenName, SurName, DisplayName
$user.SurName
write-host $user
}
prints:
Weyers
CN=w35522,OU=Standard,OU=Users,OU=Corporate,DC=we,DC=dirsrv,DC=com
FINAL UPDATE:
ok figured it out. It needed to say write-host $lastname instead!
thanks
The property which is returned by default that you are looking for is surname. That being said you have other logic issues in your script.
Get-ADUser someuser| select surname
Once in the loop you do another Get-Aduser. Also you assign a value to $lastName which is the pipe object you should be attempting to access.
If you really wanted all the lastnames in the company you would do something like this.
Get-ADUser -Filter * | Select-Object Surname
My script won't send any information to my .txt file except the headers. I want to find any display names that may contain (), /, _ and so forth. Am I not able to use * symbol to mean that I want any display name filtered that contains a "(" anywhere in the name?
#Grab some AD attributes for the specific user ID
$userid = Get-ADUser -filter {displayname -like '*(' -or displayname -like '*_' -or displayname -like '*/'} -SearchBase "OU=Corporate,DC=we,DC=dirsrv,DC=com" -Properties name, displayname, description, manager
Trying to make it show up in my txt file but still new to powershell
#Grab some AD attributes for the specific user ID
$userids = Get-ADUser -Properties name, displayname, description, manager -filter {displayname -like '*(*' -or displayname -like '*_*' -or displayname -like '*/*'}
#THIS IS THE FOREACH I'M TRYING TO MAKE WORK
foreach ($userid in $userids)
{
$ID = Get-AdUser ($userid.displayname) -Properties displayname
$userid = $ID.displayname
}
foreach ($userid in $userids)
{
#manager missing
if ($userid.Manager -eq $null) {
$owner = "MISSING"
$ownerid = "MISSING"
$ownername = "MISSING"
} else {
#grab the manager's name, surname, and department
$owner = Get-ADUser ($userid.Manager) -Properties GivenName, Surname
$ownerid = $owner.Name
$ownername = $owner.Surname + "." + $owner.GivenName
}
}
What I'm making so far. Not having good luck tho lol
When you use the -like operator like you are, you are looking for strings that end in (,_, etc. Instead you need to surround the character you are looking for with wildcards:
{displayname -like '*(*' -or displayname -like '*_(*' -or displayname -like '*/*'}
Alternatively, for a more succinct query, you could use a regular expression:
{displayname -match '[\(\)\\_]'}
Note that since (,), and \ are special regular expression characters, you have to escape them with \.
WOW so if I input the code
Get-AdUser -Properties displayname -filter {displayname -like '*(*'} | Select displayname
Then it will give me all the listings I need of the displayname..... note to self!
Now to connect it with my code :P