get-adgroupmember powershell multiple lines of code

Hi, somebody kindly provided a powershell script for listing all groups in AD, as shown below:

$Groups = get-adgroup -filter *
foreach ($Group in $Groups) {
Get-ADGroupMember -Identity $Group | select $group, name | out-file "c:\groupmembers.csv" -append

Open in new window

However, as I am not from a powershell/scripting background, I dont really get how you run this. Some of the other scripts I have seen are "one-liners", i.e. get-aduser, where I can just copy and paste them onto the powershell console and run then, but for cipts with a few lines of code, do you have to save it as a specific file, and then "run the file"? If I add all the above to a single line of text and try executing that i get errors.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HariomExchange ExpertsCommented:

Please copy the above content into notepad save the file as Name.ps1 (extenstion should be ps1)

Then Open Exchange Managment Shell from There you can run the above .ps1 script

To Execute
[PS] C:\Path of saved Ps1 Script>.\name.ps1

For Example [PS] C:\>.\backup_report.ps1

While Executing script add .\ on the starting of script.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Even without saving it to file, you should just be able to copy the above code and paste it as is to PS console to run it (may have to press Enter a couple times after).
Will SzymkowskiSenior Solution ArchitectCommented:
If you are running this as .ps1 then you are also going to need to change the Execution Policy as well. Restricted is the default Execution Policy and you will get errors trying to run the script.

Run Get-ExecutionPolicy

If you run this and it is set to Restricted then run the below command to change it.

Get-ExecutionPolicy | Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.