# How to set environment through script?

**URL:** <https://community.postman.com/t/how-to-set-environment-through-script/39587>\
**Category:** Help Hub\
**Tags:** environments\
**Created:** [September 29, 2022, 8:34am UTC](https://community.postman.com/t/how-to-set-environment-through-script/39587 "2022-09-29T08:34:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![usman246](https://avatars.discourse-cdn.com/v4/letter/u/9e8a1a/32.png) [@usman246](https://community.postman.com/u/usman246)\
**Post date:** [September 29, 2022, 8:34am UTC](https://community.postman.com/t/how-to-set-environment-through-script/39587/1 "2022-09-29T08:34:35Z")

</div>

Hi,

Just wondering if there is a way we can select an environment through script without any manual intervention?

Typically a code snippet in Pre-requisite Script would select the environment prior to running a collection.

---

<div class="post-metadata">

**Author:** ![imirza10](https://avatars.discourse-cdn.com/v4/letter/i/8dc957/32.png) [@imirza10](https://community.postman.com/u/imirza10)\
**Post date:** [September 29, 2022, 10:44am UTC](https://community.postman.com/t/how-to-set-environment-through-script/39587/2 "2022-09-29T10:44:33Z")

</div>

Yes you can do that. In the Pre-request Script tab of the relevant artifact use the following script:

```auto
pm.collectionVariables.set('environment', '<your-value>');

```

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [September 29, 2022, 11:56am UTC](https://community.postman.com/t/how-to-set-environment-through-script/39587/3 "2022-09-29T11:56:12Z")

</div>

@imirza10

All that does is set a collectionVariable called “Environment”.

The original post asked if it possible to set the environment using a script.

![image](https://us1.discourse-cdn.com/getpostman/original/3X/5/3/531e6e2e8f14bde7ca91668f5f45c0614f847819.png)

@usman246

I can’t understand why you would want to do this. You would then have to update your script if you wanted to change your environment? You would be better off just using collectionVariables instead.

---

<div class="post-metadata">

**Author:** ![usman246](https://avatars.discourse-cdn.com/v4/letter/u/9e8a1a/32.png) [@usman246](https://community.postman.com/u/usman246)\
**Post date:** [September 30, 2022, 4:55am UTC](https://community.postman.com/t/how-to-set-environment-through-script/39587/4 "2022-09-30T04:55:43Z")

</div>

Yes, I see the downside of hard coding it to only run against specific environment and rework.

Similarly, defining the variables at collection level would have me separate collection for dev and systest environments. That would also complicate things. Unless im missing something.

---

<div class="post-metadata">

**Author:** ![imirza10](https://avatars.discourse-cdn.com/v4/letter/i/8dc957/32.png) [@imirza10](https://community.postman.com/u/imirza10)\
**Post date:** [September 30, 2022, 8:26am UTC](https://community.postman.com/t/how-to-set-environment-through-script/39587/5 "2022-09-30T08:26:37Z")

</div>

Regarding collection variables, you don’t need separate collection targeting different environments. I am using only one set of collection variables, have organized my folders for different environments and in the Pre Request Script tab of each folder, setting up proper values is working right for me.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [September 30, 2022, 9:25am UTC](https://community.postman.com/t/how-to-set-environment-through-script/39587/6 "2022-09-30T09:25:16Z")

</div>

This is where Postman could do with future enhancements.

I would prefer to see test suite → test case → test scripts rather than collections and folders. I’m sure they could add this in addition to the collections and folders, not as a direct replacement.

You should have the ability to tag the test cases (not the script) with the environment they are allow to run against. A test case may have multiple steps\scripts so this seems to be the appropriate place for any tags.

If the environment is set to PROD, but the test case isn’t tagged with PROD, then it won’t run.

This way, it should be one set of test data, and one set of tests to maintain across the environments.

At the moment, you have to jumps through hoops to apply any tagging to your tests. Which is usually basic functionality in an automated test suite.

---

<div class="post-metadata">

**Author:** ![usman246](https://avatars.discourse-cdn.com/v4/letter/u/9e8a1a/32.png) [@usman246](https://community.postman.com/u/usman246)\
**Post date:** [October 3, 2022, 1:40am UTC](https://community.postman.com/t/how-to-set-environment-through-script/39587/7 "2022-10-03T01:40:30Z")

</div>

Thanks. I echo your thoughts as well.

---

<div class="post-metadata">

**Author:** ![usman246](https://avatars.discourse-cdn.com/v4/letter/u/9e8a1a/32.png) [@usman246](https://community.postman.com/u/usman246)\
**Post date:** [October 3, 2022, 1:41am UTC](https://community.postman.com/t/how-to-set-environment-through-script/39587/8 "2022-10-03T01:41:11Z")

</div>

Thanks for your thoughts.
