foreach find, disable, move ADUser

Welcome Forums General PowerShell Q&A foreach find, disable, move ADUser

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
3 months, 2 weeks ago.

  • Author
    Posts
  • #105514

    Participant
    Points: 48
    Rank: Member

    I wrote this because I want to find all my samAccoutname in my csv to disable them, then move them.

    $CleanupUsers = Import-Csv -Path .\CleaupUsers.csv
    
    ForEach ($user in $CleanupUsers) {
        Get-ADUser $($user.samAccountName) |
        Disable-ADAccount -Identity $($user.samAccountName) |
        Move-ADObject -Identity $($user.samAccountName) -TargetPath "OU=DisabledUsers,DC=corp,DC=com" 
    }

    ..it's not working as I expect. Help says -Identity for Disable-ADAccount and Move-ADObject accepts pipeline By Value but not sure how to generate that.

    Some progress. I just piped Get-ADuser into disable-ADAccount:

    $CleanupUsers = Import-Csv -Path .\CleaupUsers.csv
    
    ForEach ($user in $CleanupUsers) {
        Get-ADUser $($user.samAccountName) |
        Disable-ADAccount |
        #Move-ADObject -Identity $($user.samAccountName) -TargetPath "OU=DisabledUsers,DC=corp,DC=com" 
    }
    
    ...have to figure out Move-ADObject next. I somehow have to get it to see a DN foreach object, right?
  • #105515

    Participant
    Points: 135
    Helping Hand
    Rank: Participant

    If you like to pipe something to a cmdlet you have to have something to pipe to it. 😉 So you should make sure the cmdlet you're using to feed the next cmdlet produces any output. Read the help for Disable-ADAccount please. ... and then add -PassThru to it.
    You should read the help for the cmdlets you're about to use COMPLETELY including the examples.

    • #105517

      Participant
      Points: 48
      Rank: Member

      ok I see now "By default (i.e. if -PassThru is not specified), this cmdlet does not generate any output."

      thank you Olaf

The topic ‘foreach find, disable, move ADUser’ is closed to new replies.