Powershell GUI How can I add column to GridView - powershell

This is part of a much larger script 1443 lines to be exact. it pulls the username from AD based on first and last name. I need to also have it pull the Office name from AD to help better identify users with same name. I am sure I am just missing something simple.
function getacctname {
$fname = $FirstName.Text
$lname = $LastName.Text
Try {
$User.Text = Get-ADUser -Filter "GivenName -eq '$fname' -and SurName -eq '$lname'" |
Select-Object -ExpandProperty 'SamAccountName' |
Out-Gridview -Title 'Windows Logon' -PassThru
$Email.Text = (Get-ADUser $User.text -Properties mail).mail
}

Out-GridView dynamically builds its columns based on the input data you feed to it - so in order to get 3 columns, create an object with 3 properties!
Change the Select-Object statement so that it creates an object with properties corresponding to your desired columns and Out-GridView takes care of the rest:
Get-ADUser -Filter "GivenName -eq '$fname' -and SurName -eq '$lname'" -Properties physicalDeliveryOfficeName |
Select-Object 'SamAccountName',#{Name='Office';Expression={$_.physicalDeliveryOfficeName}} |
Out-Gridview -Title 'Windows Logon' -PassThru
If the office name is stored in a different attribute, replace the two occurrances of physicalDeliveryOfficeName with the ldap display name of the attribute in question

Related

List properties of users using foreach comment

My goal is to list extended properties of a list of users by Display Name or SamAccountName pulling those names from a Csv. I am using the following script and it works but it either skips names in the Csv or repeats them. If I do one at a time it returns what I need but from the Csv it doesn’t. Csv has one column named Name.
Import-Csv C:\Users\Administrator\Documents\test.txt | Foreach {
Get-ADUser -Filter "DisplayName -eq '$($_.Name)'" -Properties *
} | Select-Object DisplayName, SamAccountName, Title, Department, EmailAddress, ObjectGUID | Sort-Object Displayname | FT
There is nothing wrong with your current code, except for using Import-Csv on a .txt file (test.txt), I would assume this was a typo. I've added an if condition to help you troubleshoot so at least you would know which users where not found.
You should also avoid the use of -Properties *, querying all properties for the users is inefficient and slow.
$properties = #(
'DisplayName'
'SamAccountName'
'Title'
'Department'
'EmailAddress'
'ObjectGUID'
)
Import-Csv C:\Users\Administrator\Documents\test.csv | ForEach-Object {
$adUser = Get-ADUser -Filter "DisplayName -eq '$($_.Name)'" -Properties $properties
if(-not $adUser) {
Write-Warning "'$($_.Name)' could not be found on AD"
return # Go next
}
$adUser
} | Select-Object $properties | Sort-Object Displayname | Format-Table

Find AD user information with DisplayName

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

Pull User Info From AD

I'm trying to pull a list of users (first name and last), their email addresses, and which container they're in, but unfortunately it's not coming up with the data I need. I can get most of the information, just not the container name.
Get-ADUser -SearchBase 'DC=DOMAINNAME,DC=COM' -Filter {(mail -ne "null") -and (Enabled -eq "true")} -Properties emailAddress |
Select givenName,surName,OU,emailAddress |
Format-Table -AutoSize |
Out-File 'C:\Users\username\Desktop\Lists\Users_List.txt'
You're looking for the attribute DistinguishedName, but to just get the OU you would have to do some formatting. If you ran Get-ADUser | Get-Member you would see there is no property called OU.
Get-ADUser `
-SearchBase 'DC=DOMAINNAME,DC=COM' `
-Filter {(mail -ne "null") -and (Enabled -eq "true")} `
-Properties emailAddress `
| Select givenName,surName,#{Name='OU';Expression={$_.DistingishedName.Replace("CN=$($_.Name),","")}},emailAddress `
| Format-Table -AutoSize `
| Out-File 'C:\Users\username\Desktop\Lists\Users_List.txt'
Article on Understanding PowerShell Custom Properties with the Select-Object cmdlet
I would change your mail filters to $false, and $true. I don't have a computer with the AD module installed on it to test out my answer, otherwise I would have provided more.

Powershell Assign MobilePhone property from Active Directory to variable

I have a small script I wrote but I am wondering if this is the correct way to pull the MobilePhone property from Active Directory using powershell?
$csvfi = import-csv "C:\Users\\Documents\users.csv"
foreach($row in $csvfi)
{
$cellphone = $row.Phone
$fullname = $row.Name
$adphone = (Get-ADUser -Filter "Name -eq '$fullname'" -Properties * | Select MobilePhone).MobilePhone
Write-Host $fullname, $adphone
}
It just seems cumbersome to do -Filter, then -Properties *, then pipe to Select and then get the .MobilePhone attribute from that object.
As a side note, I just need the raw Mobile Phone number from AD, 1-AAA-NNN-NNNN so I can compare it with the cell phone number in the spread sheet users.csv.
$adphone = (Get-ADUser -Filter "Name -eq '$fullname'" -Properties MobilePhone).MobilePhone

Get-ADUser filter out specific OU, custom column

trying to get an Audit report of active users. We have an OU that I do not want to report on.
Give me all the active (enabled) AD accounts. EXCEPT in a specific OU.
Get-ADUser -Filter{enabled -eq $true} | Select-object Samaccountname,surname,givenname `
| Export-Csv -NoTypeInformation C:\scripts\ActiveUsers.csv
How can I filter out OU=Service Accounts?
I also need to have a custom column in Column A of the csv output.
Example: The word "ACME" in column A in all rows.
Thanks much
Esther
Filter on parent containers
The OU is part of the object's DistinguishedName property.
Use Where-Object to filter out objects that reside inside a certain OU by removing the first part of the DistinguishedName and comparing the rest with the DistinguishedName of the OU:
$OUDN = "OU=Service Accounts,OU=Accounts,DC=domain,DC=tld"
Get-ADUser -Filter {Enabled -eq $true} | Where-Object { $_.DistinguishedName -notlike "*,$OUDN" }
If you know the OU name, but not the full DistinguishedName, you can remove immediate child objects of the OU from the results by splitting the distinguished name into compartments and comparing the second one (the immediate parent container) to the name you want to exclude:
$OUName = "Service Accounts"
Get-ADUser -Filter {Enabled -eq $true} | Where-Object {
$ObjectCN,$ParentCN,$null = $_.DistinguishedName -split "(?<=[^\\]),"
$ParentCN -ne "OU=$OUName"
}
or exclude any object with the given OU name in its ancestral path:
$OUName = "Service Accounts"
Get-ADUser -Filter {Enabled -eq $true} | Where-Object {
$ObjectCN,$ParentCNs = $_.DistinguishedName -split "(?<=[^\\]),"
$ParentCNs -notcontains "OU=$OUName"
}
Custom property values
Select-Object supports calculated properties. You can supply a calculated property with a static expression as the first property to select, like so:
Get-ADUser | Select-Object #{Name="MyCustomColumn";Expression={"ACME"}},Name
Exported to a CSV, the above example would have the colunm headers "MyCustomColumn" and "Name" in col A and B respectively, col A holding the value "ACME" always, whereas col B would hold the individual Names of the users
This worked - thanks gang.
Get-ADUser -Filter {enabled -eq $true} | ? {$_.DistinguishedName -notlike "*,OU=Service Accounts,*"}
And the custom column:
Select-Object -Property #{n="ColumnA"; e={"ACME"}}