0

I am trying to execute the script to disable bulk user in domain server. User is disabled and I am displayed the which user account is disabled. In PowerShell output it is showing which are all id is disabled. While I am trying to display the same output in PowerShell forms. It showing only the last user id disable list. The script and screenshot for your references.

Powershell output

Forms outbox

Add-Type -AssemblyName System.Windows.Forms

$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
    Multiselect = $false # Multiple files can be chosen
    Filter      = 'SpreadSheet (*.csv)|*.csv' # Specified file types
}
 
[void]$FileBrowser.ShowDialog()

$file = $FileBrowser.FileName;

If ($FileBrowser.FileNames -like "*\*") {

    # Do something 
    Import-Csv $FileBrowser.FileNames | ForEach-Object { $samAccountName = $_."samAccountName" 
        Get-ADUser -Identity $samAccountName | Disable-ADAccount
        Start-Sleep 1

        $Result = Write-Output "Account has been disabled $samAccountName "
        # Assign Result to OutputBox
        $outputBox.Text = $Result
    }
}

The below script for disable the user account

Add-Type -AssemblyName System.Windows.Forms
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
    Multiselect = $false # Multiple files can be chosen
    Filter = 'SpreadSheet (*.csv)|*.csv' # Specified file types
}
 
[void]$FileBrowser.ShowDialog()
$file = $FileBrowser.FileName;
If($FileBrowser.FileNames -like "*\*") {
    # Do something 
    Import-Csv $FileBrowser.FileNames | ForEach-Object {$samAccountName = $_."samAccountName" 
        Get-ADUser -Identity $samAccountName | Disable-ADAccount
        Start-Sleep 1
$Result = Write-Output "Account has been disabled $samAccountName "
 # Assign Result to OutputBox
  $outputBox.Text = $Result
       
    
}
  }
}
2
  • Change $outputBox.Text = $Result to $outputBox.Text = $outputBox.Text,$Result -join "`r`n" Commented Apr 19, 2022 at 9:05
  • @Mathias R. Jessen Thanks it working fine Commented Apr 19, 2022 at 9:12

1 Answer 1

1

You currently overwrite the previous value when you do $outputBox.Text = $Result. Make sure you include the previous value when updating the text box, like so:

$outputBox.Text = $outputBox.Text,$Result -join "`r`n"

This way, each successive update will add a newline and then the new value to the text box, so it acts more like the screen buffer in the console.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.