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.
LVL 3
pma111Asked:
Who is Participating?
 
HariomExchange ExpertsCommented:
Hi,

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.
0
 
footechCommented:
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).
0
 
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

Will.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.