none
Powershell - Active Directory - Add line numbers RRS feed

  • Question

  • Hello all,

    I am new to powershell scripting and am currently working with Active Directory. I have what I believe to be a simple problem that I believe this community can help with, but can't seem to get it myself. I am fetching around ~3000+ AD users and they will exported into a csv sheet, however, in the powershell I want to show who is being processed and what number out of that ~3000+ is currently being processed. The users are different each line, but I can't get the number to increment past 1. Below is what I have:

    $allusers = get-aduser -Filter * -Properties *
    
    $users = $allusers | Where-Object { $_.Enabled -eq $true }
    
    
    $today = get-date
    
    $maxPWAge = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge.Days
    
    $results = @()
    $i = 1
    $total = $users.count
    
    foreach ($user in $users) {
        "$i / $total -> Processing $($user.Samaccountname)"

    This is an example output that I get in powershell:

    1 / 3057 -> Processing "samaccountname"

    Monday, June 10, 2019 3:59 PM

Answers

  • You have to write code to increment the numner

    Either:

    $i++

    or

    $i = $i + 1

    Simple arithmetic.


    \_(ツ)_/

    • Marked as answer by crdavis10 Monday, June 10, 2019 5:15 PM
    Monday, June 10, 2019 4:48 PM

All replies

  • You have to write code to increment the numner

    Either:

    $i++

    or

    $i = $i + 1

    Simple arithmetic.


    \_(ツ)_/

    • Marked as answer by crdavis10 Monday, June 10, 2019 5:15 PM
    Monday, June 10, 2019 4:48 PM
  • That's what I originally thought, but it doesn't increment the number at all, which is why I am confused. The powershell will output this everytime:

    1 / 3057 -> Processing "samaccountname"

    Monday, June 10, 2019 5:00 PM
  • There is no place in your code where you are incrementing anything.  Without the code noting will be incremented.


    \_(ツ)_/

    Monday, June 10, 2019 5:08 PM
  • Oh my. Sorry, I put the above code from an older file. With the new one it was this:
    $allusers = get-aduser -Filter * -Properties *
    
    $users = $allusers | Where-Object { $_.Enabled -eq $true }
    
    $today = get-date
    
    $maxPWAge = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge.Days
    
    $results = @()
    $i = $i + 1
    $total = $users.count
    
    foreach ($user in $users) {
        #For powershell only. Shows where it's at with processing users
        "$i / $total -> Processing $($user.Samaccountname) i++"
    

    It was formatted wrong and I mistakenly had it in the parentheses. I think it's time for bed. Thank you for the help
    Monday, June 10, 2019 5:15 PM
  • If that's all the code, there are problems: The foreach statement is missing a closing } character, and you are still not incrementing your counter variable inside the foreach loop.


    -- Bill Stewart [Bill_Stewart]

    Monday, June 10, 2019 5:26 PM
    Moderator