# Script that collect information about connected user

**URL:** <https://forums.powershell.org/t/script-that-collect-information-about-connected-user/17416>\
**Category:** PowerShell Help\
**Created:** [August 25, 2021, 2:33pm UTC](https://forums.powershell.org/t/script-that-collect-information-about-connected-user/17416 "2021-08-25T14:33:20Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Houssemjo](https://avatars.discourse-cdn.com/v4/letter/h/57b2e6/32.png) [@Houssemjo](https://forums.powershell.org/u/Houssemjo)\
**Post date:** [August 25, 2021, 2:33pm UTC](https://forums.powershell.org/t/script-that-collect-information-about-connected-user/17416/1 "2021-08-25T14:33:20Z")

</div>

Hello guys,

This time i tried to write a script that collect information about local administrator Users, print it in .txt named with the name of the laptop + the creation date. than send it to a local repertory and somewhere else on a NAS.

the script work perfectly on my laptop but not on other laps i had diffrent error like :

```auto
Get-LocalGroupMember: Impossible de comparer deux éléments dans le tableau.

Corbeille Au caractère C:\Users\bedouia\Downloads\Utilisateur envoie Utilisateur envoie listeutilisateur.ps1:33: 29 e Utilisateur = Get-Local GroupMember -Name "Administrateurs" Selec ...

: NotSpecified: (:) [Get-LocalGroupMember), InvalidoperationException + FullyqualifiedErrorId: Une erreur non spécifiée s'est produite., Microsoft PowerShell.Commands.GetLocalGroupMemb

+ CategoryInfo

erCommand

Google Chrom 163.*.*.73\Adminlocal compte : Le terme «\\163.*.*.73\Adminlocalcomptes n'est pas reconnu comme nom d'applet de commande, fonction, fichier de script ou programme exécutable. Vérifiez l'orthographe du nom, ou si un chemin d'acces existe, vérifiez que le chemin d'accès est correct et réessayez. I Au caractère C:\Users\bedouia Downloads\Utilisateur envoie utilisateur envoie listeutilisateur.ps1:59: 9

Suri=\\163.*.*.73\Adminlocal compte

: ObjectNotFound: (\\163.*.*.73​​​​​​​\Adminlocal compte:String), CommandNotFoundException FullyqualifiedErrorId: CommandNotFoundException

CategoryInfo

```

My script is :

```auto

        $Nam = Get-CimInstance -ClassName Win32_ComputerSystem | Select Name
        $Sam = $Nam.Name
        $date =Get-Date -uformat "%Y%m%d"
        $fichier = $Sam+"_"+$date+".txt"

function Liste_Utilisateurs {
    Begin {

        $userid = $env:username
        $fichier = $Sam+"_"+$date+".txt"
    }
    Process {
     
      $Liste_Utilisateur = Get-LocalGroupMember -Name "Administrateurs" | Select Name 
    }
    End {
            Return $Liste_Utilisateur
    }
}

Liste_Utilisateurs | Out-File $fichier 

#######################
# #
# Envoie de fichier #
# #
#######################
 
 #Envoie le fichier localement 

 $uri ='C:\Windows\Temp'
  Copy-Item $fichier $uri

 #envoie le fichier sur le Nas 
 $uri = \\163.*.*.*.*\Adminlocalcompte
 Copy-Item $fichier $uri 
 
  

  pause 

```

and i put a little bat script to lunch my powershell script with a bypass policy

```auto
@ECHO OFF

SET ThisScriptsDirectory=%~dp0

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%ThisScriptsDirectory%listeutilisateur.ps1""' -Verb RunAs}"

```

any idea why is this happening

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 25, 2021, 2:57pm UTC](https://forums.powershell.org/t/script-that-collect-information-about-connected-user/17416/2 "2021-08-25T14:57:51Z")

</div>

I think you are overcomplicating this. This should be all you need actually:

```auto
$FileName = $env:COMPUTERNAME + $(Get-Date -Format '_yyyyMMdd') + '.txt'
$FilePath = Join-Path -Path $([System.IO.Path]::GetTempPath()) -ChildPath $FileName

(Get-LocalGroupMember -Name 'Administrateurs').Name | Out-File -FilePath $FilePath

$uri = '\\163.*.*.*\Adminlocalcompte'
Copy-Item -Path $FilePath -Destination $uri 

```

How do you like to run this script? If a user is supposed to run it you could create a shortcut with the following command line:

```auto
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -ExecutionPolicy Bypass -File 'CompletePath\Script.ps1'

```

---

<div class="post-metadata">

**Author:** ![Houssemjo](https://avatars.discourse-cdn.com/v4/letter/h/57b2e6/32.png) [@Houssemjo](https://forums.powershell.org/u/Houssemjo)\
**Post date:** [August 25, 2021, 3:07pm UTC](https://forums.powershell.org/t/script-that-collect-information-about-connected-user/17416/3 "2021-08-25T15:07:20Z")

</div>

yes a User should run this script with a simple click that’s why i made a bat luncher with this commande line

```auto
@ECHO OFF

SET ThisScriptsDirectory=%~dp0

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%ThisScriptsDirectory%listeutilisateur.ps1""' -Verb RunAs}"

```

it will be in the same file with my script

---

<div class="post-metadata">

**Author:** ![Houssemjo](https://avatars.discourse-cdn.com/v4/letter/h/57b2e6/32.png) [@Houssemjo](https://forums.powershell.org/u/Houssemjo)\
**Post date:** [September 1, 2021, 7:58am UTC](https://forums.powershell.org/t/script-that-collect-information-about-connected-user/17416/4 "2021-09-01T07:58:23Z")

</div>

hi again,  
A user tried to run the script but he recive this error every time

```auto
Get-LocalGroupvember: Impossible de comparer deux éléments dans le tableau.

Au caractere C:\Users\bedouta Downloads Utilisateur (Get-LocalGroupMesiber -Name Administrateurs'). Name

envoie utilisateur envoiellisteutilisateur.ps1:4 Out-File -FileP

: 2

: Not Specified: (:) [Get-LocalGroupMember], InvalidoperationException FullyQualifiedErrorId: Une erreur non spécifiée s'est produite.,Microsoft.PowerShell.Commands.GetLocalGroupMenti

CategoryInfo

```

Any idea !?

Note : this script work perfectly on my lap but not on others.  
Thanks in advance

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [September 1, 2021, 2:27pm UTC](https://forums.powershell.org/t/script-that-collect-information-about-connected-user/17416/5 "2021-09-01T14:27:54Z")

</div>

Maybe this:  
Note

The Microsoft.PowerShell.LocalAccounts module is not available in 32-bit PowerShell on a 64-bit system.

From here:

> **[Get-LocalGroupMember (Microsoft.PowerShell.LocalAccounts) - PowerShell](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.localaccounts/get-localgroupmember?view=powershell-5.1)**
>
> The Get-LocalGroupMember cmdlet gets members from a local group. Note The Microsoft.PowerShell.LocalAccounts module is not available in 32-bit PowerShell on a 64-bit system.

---

<div class="post-metadata">

**Author:** ![Chen.Chen](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/chen.chen/32/1689_2.png) [@Chen.Chen](https://forums.powershell.org/u/Chen.Chen)\
**Post date:** [September 2, 2021, 12:39am UTC](https://forums.powershell.org/t/script-that-collect-information-about-connected-user/17416/6 "2021-09-02T00:39:25Z")

</div>

@Houssemjo  
Hello Hou,

Why don’t you use the invoke-command to run this script in your computer?  
then retun all computernames and administrators member.

---

<div class="post-metadata">

**Author:** ![Houssemjo](https://avatars.discourse-cdn.com/v4/letter/h/57b2e6/32.png) [@Houssemjo](https://forums.powershell.org/u/Houssemjo)\
**Post date:** [September 2, 2021, 12:23pm UTC](https://forums.powershell.org/t/script-that-collect-information-about-connected-user/17416/8 "2021-09-02T12:23:44Z")

</div>

hi tony, actualy im using 64-bit PowerShell not 32-Bit

---

<div class="post-metadata">

**Author:** ![Houssemjo](https://avatars.discourse-cdn.com/v4/letter/h/57b2e6/32.png) [@Houssemjo](https://forums.powershell.org/u/Houssemjo)\
**Post date:** [September 2, 2021, 12:25pm UTC](https://forums.powershell.org/t/script-that-collect-information-about-connected-user/17416/9 "2021-09-02T12:25:51Z")

</div>

@Chen.Chen im still begginer with scripting, actualy im using a bat script to run my script, and i want to return all users on a computer not computernames !!

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [September 2, 2021, 1:18pm UTC](https://forums.powershell.org/t/script-that-collect-information-about-connected-user/17416/10 "2021-09-02T13:18:22Z")

</div>

> [@Houssemjo](#):
>
> i want to return all users on a computer

I just want to mention that a user does not have to be a member of the local administrators group. So you might use the wrong query for what you’re actually after. 😉

---

<div class="post-metadata">

**Author:** ![Houssemjo](https://avatars.discourse-cdn.com/v4/letter/h/57b2e6/32.png) [@Houssemjo](https://forums.powershell.org/u/Houssemjo)\
**Post date:** [September 2, 2021, 1:54pm UTC](https://forums.powershell.org/t/script-that-collect-information-about-connected-user/17416/11 "2021-09-02T13:54:49Z")

</div>

@Olaf the porpuse of this script is to verify that the user has a local admin account so that’s why im looking for all the users that exist on the lap because there was other acount but admin on the domain but not localy.  
like those one

```auto
Name
----
Local
FR-L3032727\"""admin_houssemeddine"""
FR-L3032727\Administrateur
FR-L3032727\Support
Domain 
GROUPINFRA\Domain Admins
GROUPINFRA\"""houssemeddine.benyou"""
GROUPINFRA\seba.durand
GROUPINFRA\T-Country-FR-PCAdministrators

```

but i dont know why the commande work on my lap but not on others.  
Thanks in advance.

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [September 2, 2021, 2:42pm UTC](https://forums.powershell.org/t/script-that-collect-information-about-connected-user/17416/12 "2021-09-02T14:42:39Z")

</div>

OK, I get it, but you mention it works for you and NOT others, are the others also using 64bit?

---

<div class="post-metadata">

**Author:** ![Houssemjo](https://avatars.discourse-cdn.com/v4/letter/h/57b2e6/32.png) [@Houssemjo](https://forums.powershell.org/u/Houssemjo)\
**Post date:** [September 2, 2021, 2:50pm UTC](https://forums.powershell.org/t/script-that-collect-information-about-connected-user/17416/13 "2021-09-02T14:50:42Z")

</div>

@tonyd yes tony all users use 64-Bit system and as administrators also.

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [September 2, 2021, 3:49pm UTC](https://forums.powershell.org/t/script-that-collect-information-about-connected-user/17416/14 "2021-09-02T15:49:05Z")

</div>

I ran into this problem today.  
One of the reasons it can occur is if you have an orphaned account in the group. i.e. a SID that cannot be resolved to a deleted AD account:

> <https://github.com/PowerShell/PowerShell/issues/2996>
>
> It appears that if you have domain groups added to a local group and then move t…he server to a workgroup before removing them Get-LocalGroupMember no longer works and instead throws an exception 'Failed to compare two elements in the array.'. I am seeing this on Server 2016 Core, I have not tried on any other editions. I found this while building a server in one domain that needed to move to a different one.  
> 
> Steps to reproduce
> \------------------
> 
> \- Join a Windows Server 2016 Core server to a domain.
> \- Added 3 domain groups to the local administrators group.  
> \- Moved the server back into a workgroup.  
> \- Ran \`Get-LocalGroupMember -Group Administrators\`
> 
> I've reproduced this on two servers.
> 
> Expected behavior
> \-----------------
> It should return the group members as it would prior to moving to a workgroup and just display the unresolved SIDs for the old domain. Then ideally allow you to reference those unresolved SIDS for Remove-LocalGroupMember.
> 
> Actual behavior
> \---------------
> Throws an exception.
> !\[2017-01-11\_17-32-25\](https://cloud.githubusercontent.com/assets/14333618/21869108/ef2d15da-d823-11e6-94b1-7a12f2c3bb5a.png)
> 
> 
> Environment data
> \----------------
> 
> \`\`\`powershell
> \> $PSVersionTable
> PSVersion 5.1.14393.206
> PSEdition Desktop
> PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
> BuildVersion 10.0.14393.206
> CLRVersion 4.0.30319.42000
> WSManStackVersion 3.0
> PSRemotingProtocolVersion 2.3
> SerializationVersion 1.1.0.1
> 
> \`\`\`

There are different methods of auditing the local groups, but each has some limitation, various techniques are discussed here:

> **[The Admin’s First Steps: Local Group Membership](https://devblogs.microsoft.com/scripting/the-admins-first-steps-local-group-membership/)**
>
> Summary: Richard Siddaway talks about using Windows PowerShell to discover the membership of local groups.  Hey, Scripting Guy! I’ve just starting using Windows PowerShell to administer my systems, and I’ve been told I need to check the...

However, in my testing today, I’ve found the most reliable way is  
`Invoke-Command -ComputerName {net localgroup Administrators}`.

It’s a bit of a faff to parse out the unwanted text but easier than dealing with the limitations and errors being thrown by the other methods.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [September 2, 2021, 3:52pm UTC](https://forums.powershell.org/t/script-that-collect-information-about-connected-user/17416/15 "2021-09-02T15:52:17Z")

</div>

> [@Houssemjo](#):
>
> the porpuse of this script is to verify that the user has a local admin account

The proper way to achieve this would be to add a domain security group to the local administrators and add all needed domain users to this group. 😉

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [September 2, 2021, 6:55pm UTC](https://forums.powershell.org/t/script-that-collect-information-about-connected-user/17416/16 "2021-09-02T18:55:44Z")

</div>

A agree with Olaf. It also sounds like you may be adding general users to the admin group? If that is the case, you should re-think that as it is definitely not a best practice.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:21pm UTC](https://forums.powershell.org/t/script-that-collect-information-about-connected-user/17416/17 "2024-05-16T20:21:36Z")

</div>


