Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8080563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:26:58+00:00 2026-06-05T16:26:58+00:00

I’m currently writing a Powershell script that’s used to select a user from Active

  • 0

I’m currently writing a Powershell script that’s used to select a user from Active Directory, then allow you to select a computer they’ve logged into (via an SQL query) and remote desktop into it.
The user is prompted to enter a full or partial name, then a list of all matches are printed and they are prompted to select one. The list of all matches is from iterating through an array, which all matches have been assigned to. If the search from the name input produces an array with only one person, and then the user selects that one person I get the following error:

    Get-ADUser : Variable: 'u' found in expression: $u is not defined.
    At C:\Users\styanc\Desktop\test.ps1:67 char:12
    +     Get-ADUser <<<<  -f{DisplayName -eq $u} -Properties TelephoneNumber, OtherTelephone, Mobile | Select TelephoneNumber, OtherTelephone, Moblie #| Format
    -List
        + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException
        + FullyQualifiedErrorId : Variable: 'u' found in expression: $u is not defined.,Microsoft.ActiveDirectory.Management.Commands.GetADUser
The functions are as follows.
    Function FindUsers{
        param ($n)
        #creates an array of users with names LIKE the script users input
        $n = @(Get-ADUser -f {DisplayName -like $n} -Properties DisplayName) 
        return $n
    }
    Function PrintUsers{
        param ($array)
        $i = 1
        #for each user in the array, print their name
        foreach($object in $array){
            Write-Host "$i. $($object.name)"
            $i++
        }
    }
    Function SelectUser{
        #there's probably a better way to do newlines?
        Write-Host ""
        #user selects a user from the list by number, input needs validation
        $userNum = Read-Host "Please select a user. (by number)"
        $length = $usersArray.Length
        Write-Host $length
        Write-Host $usersArray.length
        if($usersArray.Length -eq $null){
            $user = ($usersArray.Name)
        }
        else{
            $user = ($usersArray[$userNum-1].Name)
        }
        #$user = ($usersArray[$userNum-1].Name)
        return $user
    }

And are called like this:

$usersArray = FindUsers -n $name
PrintUsers -array $usersArray
$selectedUser = SelectUser
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T16:26:59+00:00Added an answer on June 5, 2026 at 4:26 pm

    In function FindUsers, just try to replace :

    return $n
    

    by

    return ,$n
    

    ,$n force return command to return a list whatever $n is a single value or a list, without that return command is in the habit to transform single value array into a single value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.