Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7703545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:29:02+00:00 2026-05-31T23:29:02+00:00

I have a script which loops through a list of users (samaccountname): # Read

  • 0

I have a script which loops through a list of users (samaccountname):

# Read usersfile to variable
$users = get-content ("users.txt")

# Get current time
$now = $(get-date -uformat "%H:%M %d/%m/%Y")

# Loop through list of users
foreach($user in $users) {

    # Disable user
    Disable-QADUser $user

    # Set informative description
    Set-QADuser $user -Description "Disabled $now"

    # Delete all groupmemberships except "domain users"
    Get-QADGroup -Containsmember $user | where-object { $_.name -ne 'domain users'} | Remove-QADGroupmember

    # Move to "disabled users" group
    move-QADObject $user -NewParentContainer 'contosoc.com/Disabled users'

    # Hide from addresslist
    Set-Mailbox -identity $user -HiddenFromAddressListsEnabled $true

    # Moving mailbox to disabled users database
    Move-Mailbox -Identity $user -TargetDatabase "myserver\mydb" -BadItemLimit 50 -Confirm:$False
}

I would like to:

  • Suppress output from the different cmdlets and only show “$user is OK!” if all is ok and log success to a logfile.txt
  • Display “Error!” and the command that failed if not ok. And output the complete error msgs to a separate logfile.

I’ve been thinking about doing a if(!cmdlettorun) { write-host "Error!" } But I’m thinking that there must be a better way.

How should I do error handling in a proper fashion so I minimize the displayed output but still let me see it if desirable?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:29:04+00:00Added an answer on May 31, 2026 at 11:29 pm

    For suppressing cmlet output you can pipe to out-null or precede the command with:

    [void](.. your cmdlets..)
    

    a good way for your goal is using a tray-catch-finally code like in this minimized code:

    $a = $ErrorActionPreference 
    $ErrorActionPreference = "SilentlyContinue"
    
    foreach($user in $users) {
    
    try 
    {
    ... yours code ...
    $JobStatus = "OK"
    }
    catch [exception]
    {
       $("Error catched: " + $_.Exception.GetType().FullName) | out-file c:\file.log
       $("Error catched: " + $_.Exception.Message) | out-file c:\file.log -append
    
       $JobStatus = "not OK"
       continue; 
    }
    finally
    {
       write-host "$user is $JobStatus!"
    }
      $ErrorActionPreference = $a
    }
    

    For some hint to use try-catch-finally read here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script which loops through a GridView on my web page, and
I have an aprox 1000 line-script which loops through all servers in AD. The
I have a datatable which loops through a List and has a value column
I have a php script with an array which loops through months and displays
I have a Perl script which nests foreach loops as seen below. It takes
I have a script which will be run interactively by non-technical users. The script
I have a script which hides (display:none) certain divs in the list on page
I have a for loop to loop through my list of SliderItems which contain
For an application I'm developing I need a Perl script which loops through a
I have a script, lets call it linkchecker, that loops through about 10.000 URLs,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.