PowerShell Pipes Not Working In Visual Studio

Starnes, Daniel 20 Reputation points
2025-03-20T21:18:51.4833333+00:00

We are needing to transition some of our PowerShell scripts to Powershell 7. As part of this, I am now working in Visual Studio Code instead of PowerShell ISE. My problem is that when running script in Visual Studio, pipes don't work.

I have a script to remove all members from a group of security groups, using this line of code:

Get-ADGroupMember -server "XXXX" $Class | ForEach-Object {Remove-ADGroupMember -server "XXXX" $Class $_ -Confirm:$false}

This works if I run the script from the command line, "Powershell - file filename.ps1". But if I run it in VS I get this error:

Remove-ADGroupMember: Cannot bind parameter 'Members'. Cannot convert value "XXX" to type "Microsoft.ActiveDirectory.Management.ADPrincipal".

Error: "Cannot convert the "XXX" value of type "Deserialized.Microsoft.ActiveDirectory.Management.ADPrincipal" to type "Microsoft.ActiveDirectory.Management.ADPrincipal"."

I get this sort of error every time I try to pipe output from one cmdlet to another in VS. These scripts worked just fine in Powershell ISE, but not VS. And again, it will execute properly if I run the script from the command line. But I need to be able to make adjustments on the fly before executing.

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. MotoX80 37,161 Reputation points
    2025-03-20T22:40:13.0466667+00:00

    Get-ADGroupMember -server "XXXX" $Class | ForEach-Object {Remove-ADGroupMember -server "XXXX" $Class $_ -Confirm:$false}

    I have no way to test this, but you don't specify -identity or -members. That might be a problem. Try this.

    Get-ADGroupMember -server "XXXX" -Identity $Class | ForEach-Object {Remove-ADGroupMember -server "XXXX" -identity $Class -members $_ -Confirm:$false}
    

    https://learn.microsoft.com/en-us/powershell/module/activedirectory/get-adgroupmember?view=windowsserver2025-ps

    https://learn.microsoft.com/en-us/powershell/module/activedirectory/remove-adgroupmember?view=windowsserver2019-ps


  2. Anonymous
    2025-03-21T08:55:38.62+00:00

    Hello,

    Thank you for posting in Microsoft Q&A.

    Based on the description, I understand your question is related to PowerShell Pipes Not Working In Visual Studio.

    Check you have the latest version of the PowerShell extension for VS Code installed. You can update it from the Extensions view in VS Code.

    Check the execution policy in VS Code is set to allow script execution. You can set it using the following command:

    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

    Also, check you are using the PowerShell Integrated Console in VS Code. You can switch to it by selecting it from the terminal dropdown.

    If previous steps don't resolve the issue, you might want to consider using a different approach to manage the group members, such as exporting the members to a CSV file and then processing them.

    Have a nice day.

    Best Regards,

    Molly

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it

    0 comments No comments

  3. Starnes, Daniel 20 Reputation points
    2025-03-21T17:44:09.2966667+00:00

    Thank you all for your suggestions. It has become clear that this is not a problem with PowerShell, but a problem with Visual Studio Code. I believe I have found a workaround, and I will continue to reserch VS. I am closing this thread.


  4. Starnes, Daniel 20 Reputation points
    2025-03-21T17:50:48.7966667+00:00

    I can't run the file in VS Code. But I can run it from the command line.

    Powershell -file <path and filename>


  5. Rich Matheisen 48,026 Reputation points
    2025-03-22T02:24:03.2933333+00:00

    If you're going to mix both named and positional parameters the positional parameters should be presented first, followed by the named parameters.

    # -Identity = position 0 ($Class)
    Get-ADGroupMember $Class -server "XXXX" | 
        ForEach-Object {
            # -Identity = position 0 ($Class)
            # -Members  = position 1 ($_)
            Remove-ADGroupMember  $Class $_ -server "XXXX" -Confirm:$false
        }
    

    In your code, "XXXX" is mistaken (in the Remove-ADGroupMember) for a positional parameter, not the value to be used for the named parameter -Server. "XXXX" cannot be converted to a security principal to be used as the "identity" of the group.

    Perhaps the problem isn't with either editor but with a different version of PowerShell used by VS Code and PowerShell ISE? In any case, the real problem is that you've incorrectly mixed positional and named parameters.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.