Home > Net >  I need to display the PowerShell output data to the PowerShell form outbox
I need to display the PowerShell output data to the PowerShell form outbox

Time:04-20

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
       
    
}
  }
}

CodePudding user response:

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.

  • Related