Powershell script to Get members of AD group members with first, last, email address

I'm running a powershell script to retrieve AD users from a specific AD group and pipe specific attributes to a csv file. The script is working perfectly except for one detail. I want the script to ignore any users who have a null value in any of the values
I'm piping to the spreadsheet. Meaning that if any of the users found in the queried groups have a null value in the attributes givenname, sn or mail, ignore the user and do not pipe to the csv.
Get-ADGroupMember -identity adgroup -recursive | get-adobject -Properties givenname,sn,mail | select givenname,sn,mail |export-csv -path c:\powershell\groupmembers.csv
–NoTypeInformation

Hi,
You can pipe your user objects through ForEach-Object and then use if to verify all three properties exist. If so, output the object. If not, move to the next object. After you've processed all user objects, then pipe into Export-Csv.
EDIT: See below.
Don't retire TechNet! -
(Don't give up yet - 13,225+ strong and growing)

Similar Messages

  • How do I delete old email addresses, when I try and play some of my songs I get a not authorized pop-up with an old email address?

    How do I delete old email addresses, when I try and play some of my songs I get a not authorized pop-up with an old email address?

    Edit the list the comes up when you go to the Window menu and select "Previous Recipients".
    charlie

  • Powershell script: to get the AD Security Group Name

    I need PowerShell script that takes input: AD Security Group Name and loop
    through all web applications and their content in the farm to know where this particular group is used.

    hi
    AD groups are represented in Sharepoint as SPUser object with
    SPUser.IsDomainGroup set to true. I.e. you may use the same script which is used for users:
    Powershell script to find permissions for a specific user.
    Blog - http://sadomovalex.blogspot.com
    Dynamic CAML queries via C# - http://camlex.codeplex.com

  • Send emails from a csv file (grouping records with the same email addresses)

    Writing a script to send emails from a csv which contains record details and email addresses using the Send-Mailmessage cmdlet.
    That part is no trouble, however, the csv file contain records that can have the same email address.
    Is there a way/method I could send the records with the same email address once instead of sending individual email messages?
    Thanks
    Data in the csv file
    Record number, description, email
    1234, Test 1, [email protected]
    5678, Test 2, [email protected]
    1245, Test 3, [email protected]
    4578, Test 4, [email protected]
    $data = Import-csv c:\records.csv
    ForEach($address in $data)
    Send-mailmessage -To $address.emails -from [email protected] -subject "List of records" -Body $data -SmtpServer 192.168.1.1

    The following code does what you are looking for:
    $data = Import-csv c:\records.csv$mx = "192.168.1.1"
    $subject = "List of records"
    $from = "[email protected]"
    $data| Group-Object email|Select-Object Name, @{n='msg'; e={$_.Group| Select-Object -Property "record number", description|ConvertTo-Csv -notypeinformation|Out-String}}|
    ForEach-Object {Send-MailMessage -to $_.Name -Body $_.msg -SmtpServer $mx -Subject $subject -From $from}
    I also recommend no forcing everything into one line.  "One-liner" means "one pipeline" not putting all code on one unreadable line.
    Your code should look like this:
    $data|
    Group-Object email|
    Select-Object Name, @{
    n='msg'; e={
    $_.Group| Select-Object -Property "record number", description|
    ConvertTo-Csv -notypeinformation|Out-String
    }|
    ForEach-Object {
    Send-MailMessage -to $_.Name -Body $_.msg -SmtpServer $mx -Subject $subject -From $from
    Now we can see the code and see that you are still unnecessarily converting back and forth.
    To get the group look at how I did it.
    $body=$_.Group | Format-Table |Out-String
    Isn't that much easier?
    Once you master the pipeline these things will become second nature.  In PowerShell it is not necessary to write lots of code most of the time.
    ¯\_(ツ)_/¯

  • Powershell script to get the low disk space on the servers...

    Hi,
    We have many SQL & Sharepoint servers running on windows server. I need some Powershell script to get the low disk space on those servers and it should send an mail alert saying "Disc d:\ have less than threshold 15% free space".
    Awaiting for response.
    Thanks
    Anil

    you should be able to use this powershell script which you can run as a scheduled task.
    just change the details at the start of the script to point the script to the right location to create log files. you will need to have a plain text file containing a list of server names or ip addresses that you want to check drive space on.
    # Change the following variables based on your environment
    #specify the path to the file you want to generate containing drive space information
    $html_file_dir = "\\server\share\DriveSpace"
    #specify the path and file name of the text file containing the list of servers names you want to check drive space on - each server name on a seperate line in plain text file
    $server_file = "\\server\share\servers.txt"
    #speicfy the from address for the email
    $from_address = "[email protected]"
    #specify the to address for the email
    $to_address = "[email protected]"
    #specify the smtp server to use to send the email
    $email_gateway = "smtp.domain.com" # Can either be DNS name or IP address to SMTP server
    # The seventh line from the bottom (line 167) is used if your smtp gateway requires authentication. If you require smtp authentication
    # you must uncomment it and set the following variables.
    $smtp_user = ""
    $smtp_pass = ""
    # Change the following variables for the style of the report.
    $background_color = "rgb(140,166,193)" # can be in rgb format (rgb(0,0,0)) or hex format (#FFFFFF)
    $server_name_font = "Arial"
    $server_name_font_size = "20px"
    $server_name_bg_color = "rgb(77,108,145)" # can be in rgb format (rgb(0,0,0)) or hex format (#FFFFFF)
    $heading_font = "Arial"
    $heading_font_size = "14px"
    $heading_name_bg_color = "rgb(95,130,169)" # can be in rgb format (rgb(0,0,0)) or hex format (#FFFFFF)
    $data_font = "Arial"
    $data_font_size = "11px"
    # Colors for space
    $very_low_space = "rgb(255,0,0)" # very low space equals anything in the MB
    $low_space = "rgb(251,251,0)" # low space is less then or equal to 10 GB
    $medium_space = "rgb(249,124,0)" # medium space is less then or equal to 100 GB
    #### NO CHANGES SHOULD BE MADE BELOW UNLESS YOU KNOW WHAT YOU ARE DOING
    # Define some variables
    $ErrorActionPreference = "SilentlyContinue"
    $date = Get-Date -UFormat "%Y%m%d"
    $html_file = New-Item -ItemType File -Path "$html_file_dir\DiskSpace_$date.html" -Force
    # Create the file
    $html_file
    # Function to be used to convert bytes to MB or GB or TB
    Function ConvertBytes {
    param($size)
    If ($size -lt 1MB) {
    $drive_size = $size / 1KB
    $drive_size = [Math]::Round($drive_size, 2)
    [string]$drive_size + ' KB'
    }elseif ($size -lt 1GB){
    $drive_size = $size / 1MB
    $drive_size = [Math]::Round($drive_size, 2)
    [string]$drive_size + ' MB'
    }ElseIf ($size -lt 1TB){
    $drive_size = $size / 1GB
    $drive_size = [Math]::Round($drive_size, 2)
    [string]$drive_size + ' GB'
    }Else{
    $drive_size = $size / 1TB
    $drive_size = [Math]::Round($drive_size, 2)
    [string]$drive_size + ' TB'
    # Create the header and footer contents of the html page for output
    $html_header = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Server Drive Space</title>
    <style type="text/css">
    .serverName { text-align:center; font-family:"' + $server_name_font + '"; font-size:' + $server_name_font_size + `
    '; font-weight:bold; background-color: ' + $server_name_bg_color + '; border: 1px solid black; width: 150px; }
    .headings { text-align:center; font-family:"' + $heading_font + '"; font-size:' + $heading_font_size + `
    '; font-weight:bold; background-color: ' + $heading_name_bg_color + '; border: 1px solid black; width: 150px; }
    .data { font-family:"' + $data_font + '"; font-size:' + $data_font_size + '; border: 1px solid black; width: 150px; }
    #dataTable { border: 1px solid black; border-collapse:collapse; }
    body { background-color: ' + $background_color + '; }
    #legend { border: 1px solid black; ; right:500px; top:10px; }
    </style>
    <script language="JavaScript" type="text/javascript">
    <!--
    function zxcWWHS(){
    if (document.all){
    zxcCur=''hand'';
    zxcWH=document.documentElement.clientHeight;
    zxcWW=document.documentElement.clientWidth;
    zxcWS=document.documentElement.scrollTop;
    if (zxcWH==0){
    zxcWS=document.body.scrollTop;
    zxcWH=document.body.clientHeight;
    zxcWW=document.body.clientWidth;
    else if (document.getElementById){
    zxcCur=''pointer'';
    zxcWH=window.innerHeight-15;
    zxcWW=window.innerWidth-15;
    zxcWS=window.pageYOffset;
    zxcWC=Math.round(zxcWW/2);
    return [zxcWW,zxcWH,zxcWS];
    window.onscroll=function(){
    var img=document.getElementById(''legend'');
    if (!document.all){ img.style.position=''fixed''; window.onscroll=null; return; }
    if (!img.pos){ img.pos=img.offsetTop; }
    img.style.top=(zxcWWHS()[2]+img.pos)+''px'';
    //-->
    </script>
    </head>
    <body>'
    $html_footer = '</body>
    </html>'
    # Start to create the reports file
    Add-Content $html_file $html_header
    # Retrieve the contents of the server.txt file, this file should contain either the
    # ip address or the host name of the machine on a single line. Loop through the file
    # and get the drive information.
    Get-Content $server_file |`
    ForEach-Object {
    # Get the hostname of the machine
    $hostname = Get-WmiObject -Impersonation Impersonate -ComputerName $_ -Query "SELECT Name From Win32_ComputerSystem"
    $name = $hostname.Name.ToUpper()
    Add-Content $html_file ('<Table id="dataTable"><tr><td colspan="3" class="serverName">' + $name + '</td></tr>
    <tr><td class="headings">Drive Letter</td><td class="headings">Total Size</td><td class="headings">Free Space</td></tr>')
    # Get the drives of the server
    $drives = Get-WmiObject Win32_LogicalDisk -Filter "drivetype=3" -ComputerName $_ -Impersonation Impersonate
    # Now that I have all the drives, loop through and add to report
    ForEach ($drive in $drives) {
    $space_color = ""
    $free_space = $drive.FreeSpace
    $total_space = $drive.Size
    $percentage_free = $free_space / $total_space
    $percentage_free = $percentage_free * 100
    If ($percentage_free -le 5) {
    $space_color = $very_low_space
    }elseif ($percentage_free -le 10) {
    $space_color = $low_space
    }elseif ($percentage_free -le 15) {
    $space_color = $medium_space
    Add-Content $html_file ('<tr><td class="data">' + $drive.deviceid + '</td><td class="data">' + (ConvertBytes $drive.size) + `
    '</td><td class="data" bgcolor="' + $space_color + '">' + (ConvertBytes $drive.FreeSpace) + '</td></tr>')
    # Close the table
    Add-Content $html_file ('</table></br><div id="legend">
    <Table><tr><td style="font-size:12px">Less then or equal to 5 % free space</td><td bgcolor="' + $very_low_space + '" width="10px"></td></tr>
    <tr><td style="font-size:12px">Less then or equal to 10 % free space</td><td bgcolor="' + $low_space + '" width="10px"></td></tr>
    <tr><td style="font-size:12px">Less then or equal to 15 % free space</td><td bgcolor="' + $medium_space + '" width="10px"></td></tr>
    </table></div>')
    # End the reports file
    Add-Content $html_file $html_footer
    # Email the file
    $mail = New-Object System.Net.Mail.MailMessage
    $att = new-object Net.Mail.Attachment($html_file)
    $mail.From = $from_address
    $mail.To.Add($to_address)
    $mail.Subject = "Server Diskspace $date"
    $mail.Body = "The diskspace report file is attached."
    $mail.Attachments.Add($att)
    $smtp = New-Object System.Net.Mail.SmtpClient($email_gateway)
    #$smtp.Credentials = New-Object System.Net.NetworkCredential($smtp_user,$smtp_pass)
    $smtp.Send($mail)
    $att.Dispose()
    # Delete the file
    Remove-Item $html_file
    Regards,
    Denis Cooper
    MCITP EA - MCT
    Help keep the forums tidy, if this has helped please mark it as an answer
    My Blog
    LinkedIn:

  • PowerShell Script to get the details Like Scope,Last Deployed date and Name for a Solution Deployed in the Farm.

    Hi Experts,
    I am trying to  build a PowerShell Script to get the details Like Scope,Last Deployed date and Name for a Solution Deployed in the Farm.
    Can anyone advise on this please.
    Regards

    Get-SPSolution|Select Name,Scope,LastOperationResult,LastOperationEndTime|Export-CSV "SPInstalledSolutions.csv" -NoTypeInformation
    SPSolution properties
    Get-SPSolution
    This post is my own opinion and does not necessarily reflect the opinion or view of Slalom.

  • SQL or Powershell query to get the SCOM management group id for SCOM 2007 R2 & 2012 / 2012 R2

    Hi All,
    Can any one provide me the SQL or Powershell query to get the SCOM management group id for SCOM 2007 R2 & 2012 / 2012 R2
    I had a SQL query which will query the data from data warehouse and give the management group id but i have lost it for all 3 above.
    Gautam.75801

    Hi Gautam,
    Hope it helps:
    http://blog.tyang.org/2013/03/13/data-aggregation-field-became-empty-in-opsmgr-2007-linked-performance-report/
    http://blog.tyang.org/2012/09/05/mp-authoring-targeting-rms-or-ms/
    Natalya
    ### If my post helped you, please take a moment to Vote as Helpful and\or Mark as an Answer

  • Analytical Services failed to get user's parent group tree with Error

    Hi,
    We have a frequent errror during our weekly batch for an application.
    The context:
    - Essbase Administration Services we are using is version is 9.3.1.
    - 8 applications are calculated during the week-end. The scripts executed are exactly the same for the 8 applications.
    - For example let's say that 5 scripts are launched during the night in the batch for each application (script 1, script 2 ... script 5)
    - App1 and App2 are launched alone and before the 6 others applications as these applications database are 3 x bigger (App1 is calculated alone, then app2 is calculated alone, then app3 to app8 scripts are launched in the same time).
    The issue :
    - We don't see any issue for app3 to app8, the calculation are executed without any problem from script1 to script5.
    - But we have an error in App1 and App2 log when the bath execute script 4 and we see the following error in the server log **
    "Analytical Services failed to get user's parent group tree with Error".
    (** : we don't see any log for script 4 in the application log - it's like the server bypass script 4 to go directly from script 3 to script 5 )
    Nothing special is done in script 4 but just an aggregation of the Year dimension (using a @SUM(@RELATIVE(Year,0)) calculation.
    I think that there is may be a synchronization error with Shared Services but what is strange is that it's always for the same script 4 and the batch is launched at different time every week-end.
    Can the issue be linked to the size of the database of applications (8 Gb) and difficulties for the processor to executes aggregation in a large database volume ?

    Hi,
    According to your description, my understanding is that the error occurred when sending an email to the user in workflow.
    Did you delete the existing Connections before setting NetBiosDomainNamesEnabled?
    If not, I recommend to delete and recreate your AD connections, then set NetBiosDomainNamesEnabled to true.
    Or you can delete the original User Profile Service Application and create a new one, then set the NetBiosDomainNamesEnabled to true and start the User Profile Service Application
     synchronization.
    More reference:
    http://social.technet.microsoft.com/wiki/contents/articles/18060.sharepoint-20xx-what-if-the-domain-netbios-name-is-different-than-the-fqdn-of-the-domain-with-user-profile.aspx
    Best regards.
    Thanks
    Victoria Xia
    TechNet Community Support

  • Lightroom 4 crashes when trying to open the slideshow module. I spent over three hours with both Adobe and Apple tech support and we know it is a permission issue but have not been able to get it solved.  It started with the last upgrade to 10.8

    Lightroom 4 crashes when trying to open the slideshow module. I spent over three hours with both Adobe and Apple tech support and we know it is a permission issue but have not been able to get it solved.  It started with the last upgrade to 10.8

    Back up all data.
    This procedure will unlock all your user files (not system files) and reset their ownership and access-control lists to the default. If you've set special values for those attributes on any of your files, they will be reverted. In that case, either stop here, or be prepared to recreate the settings if necessary. Do so only after verifying that those settings didn't cause the problem. If none of this is meaningful to you, you don't need to worry about it.
    Step 1
    If you have more than one user account, and the one in question is not an administrator account, then temporarily promote it to administrator status in the Users & Groups preference pane. To do that, unlock the preference pane using the credentials of an administrator, check the box marked Allow user to administer this computer, then reboot. You can demote the problem account back to standard status when this step has been completed.
    Triple-click the following line to select it. Copy the selected text to the Clipboard (command-C):
    { sudo chflags -R nouchg,nouappnd ~ $TMPDIR.. ; sudo chown -Rh $UID:staff ~ $_ ; sudo chmod -R u+rwX ~ $_ ; chmod -R -N ~ $_ ; } 2> /dev/null
    Launch the Terminal application in any of the following ways:
    ☞ Enter the first few letters of its name into a Spotlight search. Select it in the results (it should be at the top.)
    ☞ In the Finder, select Go ▹ Utilities from the menu bar, or press the key combination shift-command-U. The application is in the folder that opens.
    ☞ Open LaunchPad. Click Utilities, then Terminal in the icon grid.
    Paste into the Terminal window (command-V). You'll be prompted for your login password, which won't be displayed when you type it. You may get a one-time warning not to screw up. If you don’t have a login password, you’ll need to set one before you can run the command. If you see a message that your username "is not in the sudoers file," then you're not logged in as an administrator.
    The command will take a noticeable amount of time to run. Wait for a new line ending in a dollar sign (“$”) to appear, then quit Terminal.
    Step 2 (optional)
    The first step should give you usable permissions in your home folder. This step will restore special attributes set by OS X on some user folders to protect them from unintended deletion or renaming. You can skip this step if you don't consider that protection to be necessary.
    Boot into Recovery by holding down the key combination command-R at startup. Release the keys when you see a gray screen with a spinning dial.
    When the OS X Utilities screen appears, select
    Utilities ▹ Terminal
    from the menu bar. A Terminal window will open.
    In the Terminal window, type this:
    resetpassword
    That's one word, all lower case, with no spaces. Then press return. A Reset Password window will open. You’re not  going to reset a password.
    Select your boot volume ("Macintosh HD," unless you gave it a different name) if not already selected.
    Select your username from the menu labeled Select the user account if not already selected.
    Under Reset Home Directory Permissions and ACLs, click the Reset button.
    Select
     ▹ Restart
    from the menu bar.

  • My iPhone is using my work email to sign in to the cloud, but my apple id and password are with my "personal" email address.  I can't get rid of my "work" email address on my iPhone 4S

    My iPhone is using my work email to sign in to the cloud, but my apple id and password are with my "personal" email address.  I can't get rid of my "work" email address on my iPhone 4S

    Hi Sister Kate,
    If you change the Apple ID you are using on an iPhone or other iOS device, there are several places you need to change it on the device. See this article -
    Apple ID: What to do after you change your Apple ID
    Thanks for using Apple Support Communities.
    Best,
    Brett L 

  • Whenever I update my iPhone software, it asks me to sign in to iCloud with an old email address.  My other devices all have the correct address.  How can I get the correct address for my iPhone?  The only Apple ID that works for logging in is my new one.

    Whenever I update my iPhone software, it asks me to sign in to iCloud with an old email address.  My other devices all have the correct address.  How can I get the correct address for my iPhone?  The only Apple ID that works for logging in is my new one.

    To change the iCloud ID you have to go to Settings>iCloud, tap Delete Account, provide the password for the old ID when prompted to turn off Find My iPhone (if you're using iOS 7), then sign back in with the ID you wish to use.  If you don't know the password for your old ID, or if it isn't accepted, go to https//appleid.apple.com, click Manage my Apple ID and sign in with your current iCloud ID.  Tap edit next to the primary email account, tap Edit, change it back to your old email address and save the change.  Then edit the name of the account to change it back to your old email address.  You can now use your current password to turn off Find My iPhone on your device, even though it prompts you for the password for your old account ID. Then go to Settings>iCloud, tap Delete Account and choose Delete from My iDevice when prompted (your iCloud data will still be in iCloud).  Next, go back to https//appleid.apple.com and change your primary email address and iCloud ID name back to the way it was.  Now you can go to Settings>iCloud and sign in with your current iCloud ID and password.

  • I have a new email address and updated it with my apple id/icloud and updated on my iPhone, but i'm getting a pop up to enter my password with my old email address listed for iCloud/Apple id.

    I have a new email address and updated my apple id/icloud  with the new address. I updated on my iPhone, but i'm keep getting a pop up to enter my password with my old email address listed for iCloud/Apple id.  How do I get it to go away?

    Did you create a NEW Apple ID or did you change the email address for your OLD Apple ID? This will affect how you update apps in the future.
    Anyway, go to Settings/iTunes&App Stores, log out, then log in with the new ID.

  • When trying to verify the email address, I get the following message. Please help. This email address is already in use or you may already have an Apple ID associated with this email address. Please try again or sign in using your existing Apple ID.

    When trying to verify the email address, I get the following message. Please help. This email address is already in use or you may already have an Apple ID associated with this email address. Please try again or sign in using your existing Apple ID.

    Me too. I try to verify and i get the same message.
    I've created 3 alternate e-mail addresses and tried creating new accounts.
    Same Result!
    This is bullsh!t. How the **** can all 4 of my attempts result in the same freakin error???

  • HT204053 I did not know my kids had set up an Itunes account for me with one user name and password.  then i got an i phone and set it up with a different email address and new password.  how can i get my accounts to merge so i can have all of my music on

    I did not know my kids had set up an Itunes account for me with one user name and password.  then i got an i phone and set it up with a different email address and new password.  how can i get my accounts to merge so i can have all of my music on my iphone

    Quote: "You cannot merge two or more Apple IDs into a single one. You can, however, use one Apple ID for iCloud services and another Apple ID for store purchases (including iTunes in the Cloud and iTunes Match). See “Using one Apple ID for iCloud and a different Apple ID for Store Purchases” above for details." See also Apple ID & iCloud FAQ: http://support.apple.com/kb/HT4895?viewlocale=en_US&locale=en_US
    You can set up your iCloud account on your iOS device under: "Settings > iCloud" and a other account for store purchases under "Settings > iTunes & App Stores". Unfortunately merging accounts is not possible but you could transfer all of your music manually via iTunes from your Mac or PC.

  • Hi my name Abukar I had an old apple ID and I had problem with signing it so I decided to make a new apple ID with a new email address, so how can I link with two apple IDs and how can I get back all my previous apps that I purchased before, I cloud stuff

    Hi my name Abukar I had an old apple ID and I had problem with signing it so I decided to make a new apple ID with a new email address, so how can I link with two apple IDs and how can I get back all my previous apps that I purchased before, I cloud stuff

    It is not possible to do that.
    Allan

Maybe you are looking for

  • Can't Install Final Cut Studio V3.0

    Hi, I'm currently using a mid-2009 13-inch Macbook Pro running OS X Mavericks 10.9.1. I'm trying to install a retail verison of Final Cut Studio V3.0 that has 2 installs left, but my Macbook ejects the first installation disk about a minute after I p

  • Change in Currency in SAP

    Hi Mine is Indian Company. At present INR is the currency for the company code 1000. However as per IFRS, my company's functional currency is getting changed to USD from INR So please let me know if its possible to change the currency of the company

  • Missing Mail Plug-ins?

    I sent an email containing a couple of jpg's. In the conversation view, my sent email has a blank space for each jpg's with the words "Missing Plug-in". Are there really some missing plug-ins or does this mean something else? Thanks HNick

  • How to install the SAP Netweaver tools

    Dear Friends, I want to develop applications thro SAP Netweaver Tools.  Can any one guide me thro installation process, since I am new to Netweaver.  Where do i get the software.  Pls help. Regards, Shankaran J +91 98404 61721

  • Creating a table structure using xsd file or excel?

    Hi, How to create a table structure of the xsd file or excel file generated from access 2000? Do you have any ideas or do you know any useful tools for this?