# Environment to Collections

**URL:** <https://community.postman.com/t/environment-to-collections/74208>\
**Category:** Help Hub\
**Tags:** variables\
**Created:** [January 22, 2025, 4:17pm UTC](https://community.postman.com/t/environment-to-collections/74208 "2025-01-22T16:17:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![adoddam](https://avatars.discourse-cdn.com/v4/letter/a/5f9b8f/32.png) [@adoddam](https://community.postman.com/u/adoddam)\
**Post date:** [January 22, 2025, 4:17pm UTC](https://community.postman.com/t/environment-to-collections/74208/1 "2025-01-22T16:17:28Z")

</div>

Hello Experts,

I need some guidance to move variables from Environment to collections how can i do it?

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [January 23, 2025, 12:11pm UTC](https://community.postman.com/t/environment-to-collections/74208/2 "2025-01-23T12:11:34Z")

</div>

Hey @adoddam 👋

Welcome to the Postman Community! :postman:

I guess the first thing I would ask is why? Is there a specific reason to make those Environment scoped variables, avaible at the Collection level?

* * *

You could do this a number of different ways, doing it manually 1 by 1 would be the slowest method 🙂

You can use a function like this in the **Pre-request script** to _copy_ the variables in your selected Environment and then replicate that in the Collections:

```auto
(function moveEnvVarsToCollectionVars() {
    // Get all environment variables
    let envVars = pm.environment.toObject();

    console.log("Environment Variables:", envVars);

    let updatedVarsCount = 0;

    for (let key in envVars) {
        if (!pm.collectionVariables.has(key)) {
            // Set the variable in the collection if it doesn't already exist
            pm.collectionVariables.set(key, envVars[key]);
            console.log(`Moved variable: ${key} = ${envVars[key]} to collection variables.`);
            updatedVarsCount++;
        }
    }

    if (updatedVarsCount > 0) {
        console.log(`${updatedVarsCount} variables moved to collection variables.`);
    } else {
        console.log("No new variables to move to collection variables.");
    }
})();

```

This will grab all the Environment variables as an object and store them in a local variable. Then it will loop through the Collection variables to check that it doesn’t contain the same key, if it doesn’t, it will create a new Collection Variable.

This method would only copy the data of the _Current Value_ so if you had an environment variable that has data only in the _Initial Value_, it would only move the Variable Key across.

Another method would be to use the Postman API and the routes ([Get an environment](https://www.postman.com/postman/postman-public-workspace/request/rdrxdqi/get-an-environment?action=share&source=copy-link&creator=1794236) and [Update a Collection](https://www.postman.com/postman/postman-public-workspace/request/augwzgq/update-part-of-a-collection?action=share&source=copy-link&creator=1794236)) available and write a script to copy those Environment values to the Collection - This method would use the Initial Value though as that’s the value that is synced.

---

<div class="post-metadata">

**Author:** ![cruxto](https://avatars.discourse-cdn.com/v4/letter/c/f0a364/32.png) [@cruxto](https://community.postman.com/u/cruxto)\
**Post date:** [January 23, 2025, 1:32pm UTC](https://community.postman.com/t/environment-to-collections/74208/3 "2025-01-23T13:32:24Z")

</div>

Cheers @danny-dainton ,

I can use that code to work around the current limitations of sharing collections with users with a viewer role.

We prefer to use Environment variables as they are way more flexible and you can hide sensitive data.

Thanks again,
