# invoke-command on old PS version

**URL:** <https://forums.powershell.org/t/invoke-command-on-old-ps-version/10358>\
**Category:** PowerShell Help\
**Created:** [March 22, 2018, 7:35am UTC](https://forums.powershell.org/t/invoke-command-on-old-ps-version/10358 "2018-03-22T07:35:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kate-ritter](https://avatars.discourse-cdn.com/v4/letter/k/e9bcb4/32.png) [@kate-ritter](https://forums.powershell.org/u/kate-ritter)\
**Post date:** [March 22, 2018, 7:35am UTC](https://forums.powershell.org/t/invoke-command-on-old-ps-version/10358/1 "2018-03-22T07:35:13Z")

</div>

Hello,

I wrote the following little script to set local admin rights on a machine.

function set-localadmin {  
[CmdletBinding()]

```
    param (
        [Parameter (Position=0,mandatory = $true)]
        $computername )

        $username=Read-Host "Please type in the emailadresse.: Test@test.com"

```

Invoke-Command -ComputerName $computername -Command {Add-LocalGroupMember -Group Administratoren -Member $Using:username }

}

so far so good.  
It works on 90% of the machines. Just sometimes I get an error message that Add-LocalGroupMember is not a known cmdlet.  
I opened a PSSession onto one of the machines where is doesn’t work and found out that it has PSv2 installed.

As far as I know, the cmdlet has been introduced with v3.

Is there a way to make this work independently on which version is installed of the client?

Thank you for your help

Kind Regards  
Kate

---

<div class="post-metadata">

**Author:** ![aapeli-hietikko](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@aapeli-hietikko](https://forums.powershell.org/u/aapeli-hietikko)\
**Post date:** [March 22, 2018, 7:38am UTC](https://forums.powershell.org/t/invoke-command-on-old-ps-version/10358/2 "2018-03-22T07:38:33Z")

</div>

Hello,

How about if you’d use old school net localgroup command to perform the action or at least test if ps version is less than 3 then use net localgroup else go with the one you have already.

---

<div class="post-metadata">

**Author:** ![rafhaelcampelo](https://avatars.discourse-cdn.com/v4/letter/r/e19adc/32.png) [@rafhaelcampelo](https://forums.powershell.org/u/rafhaelcampelo)\
**Post date:** [March 22, 2018, 8:48am UTC](https://forums.powershell.org/t/invoke-command-on-old-ps-version/10358/3 "2018-03-22T08:48:48Z")

</div>

There is the possibility of updating your powershell, enter this link: [Como atualizar o Powershell em versões mais antigas do Windows Server e Windows Cliente. - CooperaTI](http://cooperati.com.br/2017/11/01/como-atualizar-o-powershell-em-versoes-mais-antigas-do-windows-server-e-windows-cliente/)

and see if you can upgrade.

---

<div class="post-metadata">

**Author:** ![kate-ritter](https://avatars.discourse-cdn.com/v4/letter/k/e9bcb4/32.png) [@kate-ritter](https://forums.powershell.org/u/kate-ritter)\
**Post date:** [March 23, 2018, 1:20am UTC](https://forums.powershell.org/t/invoke-command-on-old-ps-version/10358/4 "2018-03-23T01:20:01Z")

</div>

Hallo,

unfortunately I cannot read the link (I dont speak … spanish? portuguese?)

I tried the following:

```
function set-localadmin {
        [CmdletBinding()]

        param (
            [Parameter (Position=0,mandatory = $true)]
            $computername )

            $username=Read-Host "type in your email adresse e.g.: Test@test.com"

Invoke-Command -ComputerName $computername -Command {net localgroup Administratoren $Using:username /add }

}
```

but now it doesn’t work at all anymore 😕  
Is there a way of running v3+ commands remotely on a machine with v2?

---

<div class="post-metadata">

**Author:** ![aapeli-hietikko](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@aapeli-hietikko](https://forums.powershell.org/u/aapeli-hietikko)\
**Post date:** [March 23, 2018, 1:50am UTC](https://forums.powershell.org/t/invoke-command-on-old-ps-version/10358/5 "2018-03-23T01:50:36Z")

</div>

I’m not sure if local admin group is different between the languages. Check it out first by just running net localgroup.  
This worked for me:

```
function set-localadmin {
        [CmdletBinding()]

         Param
    (
        # ComputerName
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        $ComputerName,

        # Account
        [Parameter(Mandatory=$true,
           ValueFromPipelineByPropertyName=$true,
           Position=1)]
        $Identity
    )

    Invoke-Command -ComputerName $ComputerName -Command {net localgroup Administrators $args[0] /add } -ArgumentList $Identity

}
```

---

<div class="post-metadata">

**Author:** ![aapeli-hietikko](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@aapeli-hietikko](https://forums.powershell.org/u/aapeli-hietikko)\
**Post date:** [March 23, 2018, 2:07am UTC](https://forums.powershell.org/t/invoke-command-on-old-ps-version/10358/6 "2018-03-23T02:07:41Z")

</div>

I took it a little further to fill my needs too:

> <https://github.com/AapeliH/PowerShell/blob/komia/Add-LocalGroupMember.ps1>

---

<div class="post-metadata">

**Author:** ![john-mongelluzzo](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@john-mongelluzzo](https://forums.powershell.org/u/john-mongelluzzo)\
**Post date:** [March 23, 2018, 7:28am UTC](https://forums.powershell.org/t/invoke-command-on-old-ps-version/10358/7 "2018-03-23T07:28:11Z")

</div>

There was a time I tried using [adsi] to read/add from/to local groups. It was early in PS v2. Try Googling  
[ADSI]“WinNT://”

I found this on a quick search:  
[https://blogs.technet.microsoft.com/heyscriptingguy/2014/10/03/adding-local-users-to-local-groups/](https://blogs.technet.microsoft.com/heyscriptingguy/2014/10/03/adding-local-users-to-local-groups/)

usually “Hey scripting Guy” is a great place to start.  
Good Luck.

---

<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:36pm UTC](https://forums.powershell.org/t/invoke-command-on-old-ps-version/10358/8 "2024-05-16T20:36:05Z")

</div>

Closed on May 16, 2024, 8:36pm UTC


