I just need to be able to trim that down to just the SMTP. Right now I get a list of all proxy addresses
Get-ADUser -filter 'Enabled -eq "True"' -Properties proxyaddresses,mail | Where {$_.proxyAddresses -like "SMTP*"} | select-object mail,#{n='SMTPproxyaddresses';e={($_.proxyaddresses | ? {$_ -match '^smtp'})-join'; '}} | Export-Csv -Encoding UTF8 -NoTypeInformation C:\Export\allusers4.csv
Output at this time :
mail SMTPproxyaddresses
user1#contoso.com SMTP:userproxy#contoso.com;SMTP:user.test#contoso.com
SMTP:testproxy#contoso.com
user2#contoso.com smtp:user2#exchn.contoso.com;SMTP:user2proxy#contoso.com
user3#contoso.com SMTP:user3proxy#contoso.com
I want to get an output like below. I just need to be able to trim that down to just the SMTP
SMTPproxyaddresses
userproxy#contoso.com
testproxy#contoso.com
user2proxy#contoso.com
user3proxy#contoso.com
You need to use -cmatch for the match to be case sensitive. As for dropping the first column, just exclude it from your select.
I believe something like this should do the trick...
Get-ADUser -Filter 'Enabled -eq "True"' -Properties proxyaddresses |
where { $_.proxyAddresses -like "SMTP*" } |
select-object #{ n='SMTPproxyaddresses';e={ ($_.proxyaddresses | where { $_ -cmatch '^SMTP'})-join'; ' -replace 'SMTP:'} } |
Export-Csv -Encoding UTF8 -NoTypeInformation C:\Export\allusers4.csv
Related
I have this code that pulls members of all distribution groups. I'm trying to get the member's email address to the same csv file as a new column instead of overwriting the DL data.
Get-ADGroup -Filter 'GroupCategory -eq "Distribution"' -Properties * | Select-Object name, mail, #{l='ManagedBy';e={$_.managedby -replace '^CN=|,.*$'}}, #{l='Members';e={$_.members -replace '^CN=|,.*$' -join ","}}, #{l='MemberOf';e={$_.memberOf -replace '^CN=|,.*$' -join ","}} | Sort-Object Name, mail, ManagedBy, Members, MemberOf | export-csv DL.csv
import-csv -Path DL.csv | ForEach-Object {
Get-ADUser -Filter {Members -like $_.members} -properties mail | Select-Object SamAccountName,Name,GivenName,mail
} | Export-csv -Path DL.csv -NoTypeInformation
I want to get canonical name for each groups as well. how can I do that ?
Here is my script :
Get-ADUser -Filter {Enabled -eq $true} -Properties * | Select displayname ,#{Name="MemberOf";Expression={($_.MemberOf | %{(Get-ADGroup $_).sAMAccountName}) -Join ";"}} | Export-Csv -Path "c:\temp\users.csv" -NoTypeInformation -Encoding UTF8
My output :
"displayname","MemberOf"
"User01","Group01;Group02;Group03"
My desired output:
"displayname","MemberOf"
"User01","Group01;Contoso.com/OU1/OU2;Group02;Contoso.com/OU21/OU52;Group03;Contoso.com/OU1/OU21/OU22"
I agree with Mathias but this is how you can do it using the code you already have. Definitely recommend you to only call the properties that you need to query.
Get-ADUser -Filter {Enabled -eq $true} -Properties Displayname,MemberOf |
Select-Object Displayname,
#{
Name="MemberOf"
Expression={
# ($_.MemberOf | ForEach-Object{
# (Get-ADGroup $_ -Properties CanonicalName).CanonicalName
# }) -Join ";"
# You can pipe $_.MemberOf to Get-ADGroup, since it's an array of
# distinguishedNames it should work fine
($_.MemberOf | Get-ADGroup -Properties CanonicalName).CanonicalName -Join ";"
}
} | Export-Csv -Path "c:\temp\users.csv" -NoTypeInformation -Encoding UTF8
An alternative to that code, using a more classical approach:
$users = Get-ADUser -Filter {Enabled -eq $true} -Properties DisplayName
$result = foreach($user in $users)
{
$params = #{
LDAPFilter = "(member=$($user.DistinguishedName))"
Properties = "CanonicalName"
}
$membership = (Get-ADGroup #params).CanonicalName -join ";"
[pscustomobject]#{
DisplayName = $user.DisplayName
MemberOf = $membership
}
}
$result | Export-Csv -Path "c:\temp\users.csv" -NoTypeInformation -Encoding UTF8
First of all, you shouldn't be using Properties * when you only need two properties.
Then, the -Filter should be a string, not a scriptblock.
With just a small adaptation to your code, this should work:
Get-ADUser -Filter "Enabled -eq 'True'" -Properties DisplayName, MemberOf |
Select-Object DisplayName,
#{Name = "MemberOf"; Expression = {
($_.MemberOf | ForEach-Object {
($_ | Get-ADGroup -Properties CanonicalName).CanonicalName -Join ";" })
}
} |
Export-Csv -Path "c:\temp\users.csv" -NoTypeInformation -Encoding UTF8
Looking at your latest comment, I believe you want the group NAME joined with the canonicalname of the group as well.
You can do this like so:
Get-ADUser -Filter "Enabled -eq 'True'" -Properties DisplayName, MemberOf |
Select-Object DisplayName,
#{Name = "MemberOf"; Expression = {
$_.MemberOf | ForEach-Object {
$group = $_ | Get-ADGroup -Properties CanonicalName
'{0};{1}' -f $group.Name, ($group.CanonicalName -join ';')
}
}
} |
Export-Csv -Path "c:\temp\users.csv" -NoTypeInformation -Encoding UTF8
I have an AD group from which I need to pull all smtp addresses from ProxyAddresses for each user into a single column. I have the script below which is only pulling the first addresses from ProxyAddresses. Some users will have two or more addresses. What can I add to my script to pull all smtp addresses from ProxyAddresses. I'm new with Powershell have have struggled to get this to work. I've spent a good part of the day googling and just can't get there. Any help would be greatly appreciated. Thanks!
<pre><Get-ADGroupMember -Identity "EDL_ProEquities Smarsh" -Recursive |
Get-ADUser -Properties Proxyaddresses |
Select-Object #{ L = "ProxyAddresses"; E = {($_.ProxyAddresses | Where-Object
{$_ -like "*smtp:*"} | ForEach-Object {$_ -replace 'smtp:'}) -join
"`r`n'`;"}} |
Export-CSV -Path "c:\temp\EDL.csv" -NoTypeInformation</pre>
You're almost there. ;-) I think you have at least two options to approach this task. Either you join all desired smtp addresses in one cell in your csv file like this:
Get-ADGroupMember -Identity 'EDL_ProEquities Smarsh' -Recursive |
Get-ADUser -Properties ProxyAddresses |
ForEach-Object {
[PSCustomObject]#{
sAMAccountName = $_.sAMAccountName
ProxyAddresses = ($_.ProxyAddresses | Where-Object { $_ -match '^smtp:' } | ForEach-Object { $_ -replace 'smtp:' }) -join ','
}
} |
Export-CSV -Path 'c:\temp\EDL.csv' -NoTypeInformation
... or you output each individual smtp address on an individual line like this:
Get-ADGroupMember -Identity 'EDL_ProEquities Smarsh' -Recursive |
Get-ADUser -Properties ProxyAddresses |
ForEach-Object {
$User = $_
$SMTPAddressList = $_.ProxyAddresses | Where-Object { $_ -match '^smtp:' } | ForEach-Object { $_ -replace 'smtp:' }
foreach ($SMTPAddress in $SMTPAddressList) {
[PSCustomObject]#{
sAMAccountName = $User.sAMAccountName
SMTPAddress = $SMTPAddress
}
}
} |
Export-CSV -Path 'c:\temp\EDL.csv' -NoTypeInformation
I have a list of users in a CSV, but I need to collect the SamAccount attribute from each user by name in the ad.
CSV model
Script
Get-ADObject -Filter 'ObjectClass -eq "user" -and userAccountControl -eq "512"' -Properties * | Select-Object SamAccountName,CN,DisplayName, | Export-CSV -Path C:\Temp\UserAccounts.csv -Encoding UTF8 -NoTypeInformation
I'm a little lost I don't know how to do a foreach using name
I am trying but without success.
Trying to get samaccountname based on Name on csv file.
Import-Csv -Path C:\Temp\userteste.csv | foreach-Object {Get-ADUser -Filter {Name -like $_.name} -Properties Name | Select-Object samAccountName}
and export to csv file.
Why use Get-ADObject and not Get-ADUser for this? The latter gives you more of the desired properties you need in the CSV.
As aside, it is wasteful to do -Properties * if all you want is a small set of user attributes.
Something like this should work:
Get-ADUser -Filter "Enabled -eq $true" -Properties DisplayName, CN |
Select-Object SamAccountName, CN, DisplayName |
Export-Csv -Path C:\Temp\UserAccounts.csv -Encoding UTF8 -NoTypeInformation
As per your comment you need to get some extra attributes of the users listed in the CSV, you can do this:
Import-Csv -Path C:\Temp\userteste.csv | ForEach-Object {
Get-ADUser -Filter "Name -like '$($_.Name)'" -Properties DisplayName, CN |
Select-Object SamAccountName, CN, DisplayName
} | Export-Csv -Path C:\Temp\UserAccounts.csv -Encoding UTF8 -NoTypeInformation
Hope that helps
I'm executing a Get-ADComputer and trying to iterate through a loop that pulls computer names from individual rooms. I'm trying to output each room to a different Excel sheet.
I'm running PowerShell Version 5:
$results = for($room=102; $room -le 110; $room++) {
Get-ADComputer -SearchBase $oubase -Properties Name, Description -Filter * |
Where-Object {$_.description -clike "*RM $Room"}
}
$results |
Select-Object Name, Description |
Export-CSV '\\Desktop\Room_Hosts.csv' -NoTypeInformation -Encoding UTF8 -Append
What do I need to do to fix the Excel sheet output?
Your post says you want an Excel sheet, but your code is outputting to a CSV. You cannot add a second sheet to a CSV. You can export different CSV files per computer object.
$results = for($room=102; $room -le 110; $room++) {
Get-ADComputer -SearchBase $oubase -Properties Name, Description -Filter * |
Where-Object {$_.description -clike "*RM $Room"}
}
$results |
Select-Object Name, Description | Foreach-Object {
$_ | Export-CSV -Path ("\\Desktop\{0}.csv" -f $_.Name) -NoTypeInformation -Encoding UTF8 -Append
If the problem is getting the domain name, you can add some code to your Select-Object command.
$results = for($room=102; $room -le 110; $room++) {
Get-ADComputer -SearchBase $oubase -Properties Name,Description,DNSHostName -Filter * |
Where-Object {$_.description -clike "*RM $Room"}
}
$results |
Select-Object Name,Description,#{n='Domain';e={$_.DNSHostName -Replace $("{0}." -f $_.Name}} |
Export-CSV '\\Desktop\Room_Hosts.csv' -NoTypeInformation -Encoding UTF8 -Append
Explanation For Retrieving Computer Object's Domain:
The DNSHostName property contains the FQDN of the computer object. So you only need to remove the host name part of that string. Here, we simply replace the hostname and the following . character with nothing. Hostname is retrieved from the Name property of the computer object. The -f operator is used to simply append the . character to the name. The Select-Object uses a hash table to calculate the domain value and store it in a property called Domain.
Alternatively, you can apply the same concepts from above for getting the domain name but use the CanonicalName of the computer object with the -Split operator.
$results = for($room=102; $room -le 110; $room++) {
Get-ADComputer -SearchBase $oubase -Properties Name,CanonicalName,Description -Filter * |
Where-Object {$_.description -clike "*RM $Room"}
}
$results |
Select-Object Name,Description,#{n='Domain';e={($_.CanonicalName -Split "/")[0]}} |
Export-CSV '\\Desktop\Room_Hosts.csv' -NoTypeInformation -Encoding UTF8 -Append