PowerShell - Move errors to file

Posted on 2014-08-22
Last Modified: 2014-08-22
Dear Experts,

I have been running this command to remove users from a certain Active Directory group, basically their names are listed in a .csv file column and from there imported, then for each the command executed and putting the errors to a .txt file:

Import-Csv C:\users\z.cseh\desktop\displaynames.csv | ForEach {remove-adgroupmember -Identi
ty "Users" -Member $_.displayname -Confirm:$false} 2>> c:\users\z.cseh\desktop\DisplayNamesErr.txt

In general that works fine, but relating the errors the issue is that it puts to the .txt file when error happens, but not showing at which .csv line or at which users. Always just repeating this pattern:

Could you please advise how to parameterise somehow to show at least at which .csv line the error happened?

Question by:csehz
    LVL 40

    Accepted Solution

    Try this code.. DisplayNamesErr.csv will have the details of error..
    Import-Csv C:\users\z.cseh\desktop\displaynames.csv | ForEach {
    $Group = $_.displayname
    remove-adgroupmember -Identity "Users" -Member $Group -Confirm:$false -EA Stop
    	New-Object PSobject -Property @{
    	GroupName = $Group
    	Error = $_.exception.message
    } | Export-Csv c:\users\z.cseh\desktop\DisplayNamesErr.csv -nti

    Open in new window

    LVL 1

    Author Closing Comment

    That is great like that the error message and also the users appeared in the file, thanks very much

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    You might have come across a situation when you have Exchange 2013 server in two different sites (Production and DR). After adding the Database copy in ECP console it displays Database copy status unknown for the DR exchange server. Issue is strange…
    Check out this infographic on what you need to make a good email signature that will work perfectly for your organization.
    In this video we show how to create an Address List in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Organization >> Ad…
    The basic steps you have just learned will be implemented in this video. The basic steps are shown to configure an Exchange DAG in a live working Exchange Server Environment and manage the same (Exchange Server 2010 Software is used in a Windows Ser…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now