Skip to content
TheChatArchiveThe Chat Archive
o3-mini· OpenAIHugging Face LMArena Human Preference 140K

Write a Powershell script that pulls all user objects from the current Active Directory…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-3-7-sonnet-20250219-thinking-32k’s answer.
  1. User

    Write a Powershell script that pulls all user objects from the current Active Directory domain, pipes names to fzf, and provide a preview of other important attributes of the user object in fzf's preview panel.

  2. Assistant

    Below is an example PowerShell script that retrieves all AD users, presents a list of their SamAccountNames to fzf, and shows a live preview (formatted details for that user) in fzf’s preview panel. For this example, we load selected user properties (you can customize these as needed) and then invoke fzf with a preview command that runs Get-ADUser again for the selected account.

    Note:
    • This script assumes you have the ActiveDirectory module loaded (or available) and that the command‑line version of fzf is installed and in your PATH.
    • You can customize which properties to preview by adjusting the parameters in both the initial query and the preview command.

    Below is the complete script:


    # Requires: ActiveDirectory module & fzf in PATH
    
    # Import the ActiveDirectory module (if not already loaded)
    Import-Module ActiveDirectory
    
    # Define the properties you care about.
    # In this example, we pull DisplayName, Mail, Department, and Title.
    $properties = @("DisplayName", "Mail", "Department", "Title")
    
    # Retrieve all AD users (you can adjust the filter if needed)
    $allUsers = Get-ADUser -Filter * -Properties $properties
    
    # For the list we display, use SamAccountName. 
    # (Alternatively, you might use DisplayName, or format both together.)
    # Here we simply pass the SamAccountName per line to fzf.
    $userList = $allUsers | Select-Object -ExpandProperty SamAccountName
    
    # Construct the fzf preview command.
    # The placeholder '{}' will be replaced with the selected SamAccountName.
    # Using -NoProfile so that the preview command runs quickly.
    # The preview runs a Get-ADUser call for that account and formats a few properties.
    $fzfPreview = 'powershell -NoProfile -Command "Get-ADUser -Identity {} -Properties DisplayName,Mail,Department,Title | Format-List SamAccountName,DisplayName,Mail,Department,Title"'
    
    # Invoke fzf.
    # The user will see a list of SamAccountNames, and as they move up/down,
    # the preview panel will show additional details for the currently highlighted user.
    $selectedUser = $userList | fzf --preview $fzfPreview
    
    # Check if a selection was made
    if ($selectedUser) {
        Write-Host "You selected: $selectedUser"
        # Optionally, retrieve the full AD user object again if you want to act on it.
        $userObject = Get-ADUser -Identity $selectedUser -Properties *
        Write-Host "User details:"
        $userObject | Format-List Name, SamAccountName, DisplayName, Mail, Department, Title
    } else {
        Write-Host "No user selected."
    }
    

    How It Works

    1. Retrieve Users:
      The script uses Get-ADUser -Filter * to pull all users from the domain along with extra properties (DisplayName, Mail, Department, Title).

    2. Prepare List for fzf:
      We extract the SamAccountName from each user. This simple list is piped to fzf so that each line is one username.

    3. fzf Preview:
      The preview command is defined so that when fzf highlights a username it runs a new PowerShell command:

      • It calls Get-ADUser with the selected SamAccountName ({} is replaced by fzf).
      • It outputs a formatted list of key attributes.
    4. Selection & Output:
      Once you make a selection in fzf, the script optionally retrieves the full user object again and prints selected fields.

    You can adjust the properties or the formatting in the preview command as needed for your environment. This script demonstrates a simple and effective way to integrate PowerShell’s AD cmdlets with the fuzzy finder fzf for interactive lookups.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.