<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-M74D8PB" height="0" width="0" style="display:none;visibility:hidden">
Loading
Skip to NavigationSkip to Main Content
0D54z00007IU0oYCATOkta Classic EngineAPI Access ManagementAnswered2026-04-01T09:00:20.000Z2021-11-16T18:11:17.000Z2021-11-17T20:56:11.000Z

vir8k (vir8k) asked a question.

Powershell moving users from one group to another in Okta

Having worked with PowerShell and Okta I can get group members and list groups but would like some help with taking group members from a group and adding them into another group. Passing the variables like I normally do with AD PowerShell doesn't work.

 


  • vir8k (vir8k)

    I should be able to modify something like this to read groups based on a filter and then add them to another group based on a different filter.

     

     

    function Export-UsersAndGroups() {

        $totalUsers = 0

        $exportedUsers = @()

        # for more filters, see https://developer.okta.com/docs/api/resources/users#list-users-with-a-filter

        $params = @{filter = 'status eq "ACTIVE"'}

        do {

            $page = Get-OktaUsers @params

            $users = $page.objects

            foreach ($user in $users) {

                $userGroups = Get-OktaUserGroups $user.id

                $groups = @()

                foreach ($userGroup in $userGroups) {

                    if ($userGroup.type -eq "OKTA_GROUP") {

                        $groups += $userGroup.profile.name

                    }

                }

                $exportedUsers += [PSCustomObject]@{id = $user.id; login = $user.profile.login; groups = $groups -join ";"}

            }

            $totalUsers += $users.count

            $params = @{url = $page.nextUrl}

        } while ($page.nextUrl)

        $exportedUsers | Export-Csv c:\users\jspit\desktop\exportedUsersGroups.csv -notype

        "$totalUsers users exported."

    }

    Expand Post
    • vir8k (vir8k)

      Thanks. I will move the question over and add some clarity to it

       

    • vir8k (vir8k)

      Also the group's rules are great but I need to do this to about 130 groups. Not untenable there but I want to expand my powershell library to perform these big functions. Thanks

       

  • k5fuw (k5fuw)

    Based on the API docs, it appears that you can't add multiple users to a group at once, only one at a time. Retrieve or build your array of users to add, then use a foreach loop to add them to the group one by one.

    • vir8k (vir8k)

      Here is what I have now. It doesn't work. I can create arrays just trying to figure out how to fit that into the code where it doesn't just create a giant list of all the users from all the first set of groups and dump them into all of the second set of groups.

       

      function get-oktaInvokeGroupMembers () {

         $groups = Invoke-Method GET "/api/v1/groups?filter=type+eq+%22APP_GROUP%22&q=Test"

          Write-Output $groups

            foreach ($group in $groups) {

              $members = Get-OktaGroupMember $group.id

            Write-Output $members

            }

         $oktagroups = Invoke-Method GET  "/api/v1/groups?filter=type+eq+%22OKTA_GROUP%22&q=Test"

         Write-Output $oktagroups

            foreach ($okta in $oktagroups) {

               $oktamembers = Get-OktaGroupMember $user.id

               PUT "/api/v1/$okta.id/users/$user.id"

            }

          }

      Expand Post
    • vir8k (vir8k)

      Can you hit me with an example of gathering an array for the users from each group? I'll work on some stuff on this end and see if I can get there because I do believe you are correct, I'm just used to working with AD/Powershell and not API's

This question is closed.
Loading
Powershell moving users from one group to another in Okta