Differrence running command interactively and inide a script

So I have a bit of an oddf one.

when I run the following command direct it runs and achieves the desired outcome ( removes all computers from a specific AD security group)

Get-ADGroupMember -Identity "Checkpoint_deployment_endpoint_test_phase" | Remove-ADPrincipalGroupMembership -MemberOf "Checkpoint_deployment_endpoint_test_phase" -Confirm:$false

Howver when I run the same command as part of a script ( the script empties out the Ad group then repopulates it based upon computer name)

It Generates an error

H:\scripts\checkpoint deployment phase script> .\checkpoint_deployment_phase_script
Remove-ADPrincipalGroupMembership : Object reference not set to an instance of an object.
At H:\scripts\checkpoint deployment phase script\checkpoint_deployment_phase_script.ps1:7 char:75
+ ... st_phase" | Remove-ADPrincipalGroupMembership -MemberOf "Checkpoint_d ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Remove-ADPrincipalGroupMembership], NullReferenceException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.NullReferenceException,Microsoft.ActiveDirectory.Management.Commands.RemoveADPrincipalGroupMembership

Any suggestion as to the cause of the difference in behaviour?

Welcome to the forum. :wave:t4:

It looks to me like you messed up the quoting. But it is hard to tell because you did not provide enough information.

I wonder that your command works at all in the first place. Remove-ADPrincipalGroupMembership is actually made for removing ONE member from one or more AD groups. I’d try it it with Remove-ADGroupMember. This is supposed to remove one or more members from ONE AD group.