How to access --env-var variable in pre-request script?

Archived topic — this may be out of date.
Newman has been replaced by the Postman CLI, Postman’s official, signed command-line tool. It runs collections as Newman did, and also lints API specs, runs mocks and monitors, and syncs local files with Postman from your terminal, CI/CD pipeline or coding agent.
Working with an AI coding agent? The Postman plugin lets agents in Claude Code, Cursor, Codex and others design, mock, test and ship APIs with Postman.
For current help, see the Postman docs, try the free Postman tools (no account needed), or join us on Discord.

My question:
How to access environment variable passed from command line in pre-request script?
command I am using:

newman run ‘collection.json’ --env-var “Key=Value”

In pre-request script:
pm.environment.get(“Key”)

Value for above expression is undefined .

Details :
I wanted to pass secrets from pipeline rather than hardcoding in the newman script. Its best practise to keep secrets out of code repository. So I am using environment variable.
Postman version to create collection: v10.8.0
newman npm version: 5.3.2

How I found the problem:
When specified env var in command line and tried to extract it in pre-request script.

I’ve already tried:
I have tried global variables in command line as well, not working it either.

Any help much appreciated.

It looks like you are running the collection without an environment.

Don’t you need to include the environment in the newman command line before you can include the environment variable.

–env-var CLI option provides a way to specify env var dynamically. Please correct if that’s not true.

Yes, but you have to have an environment set for it to be added to. Otherwise where is it going to store the variable?

Thank you @michaelderekjones its working after specifying env file path. I misunderstood the --env-var usage I guess.
Below command worked and pre-request can access cli env param.
newman run ‘collection.json’ -e ‘environmentfile.json’ --env-var “Key=Value”