Quick PowerShell Help

DeaconFrost

[H]F Junkie
Joined
Sep 6, 2007
Messages
11,215
I have a list of similar cmdlets to run, each one outputting a text file. The cmdlet runs fine by itself. Is there a way to have the list run as a script (ps1) that runs each line and generates each file, so I don't have to copy and paste each one manually?

The cmdlet is Get-ADGroupMember -Identity 'Group Name' | Select Name | Out-File "File Path".

So I would have the following:

Get-ADGroupMember -Identity 'Group1' | Select Name | Out-File "C:\Temp\Group1.txt"
Get-ADGroupMember -Identity 'Group2' | Select Name | Out-File "C:\Temp\Group2.txt"

...and so on, so that in the end, if I had 35 lines, I'd end up with 35 text files.
 

Midvalley

n00b
Joined
Jul 5, 2007
Messages
42
Yep, use the Powershell ISE that's included in Windows, put the commands on separate lines and save as a PS1.

Actually, just use the ISE for everything. Has a ton of help features and cmdlet autocomplete etc built in.
 

DeaconFrost

[H]F Junkie
Joined
Sep 6, 2007
Messages
11,215
I have yet to venture into the ISE, but I think I get the drift. Thanks for the info!
 

socK

2[H]4U
Joined
Jan 25, 2004
Messages
3,913
Don't bother with ISE, get VS Code and install the preview version of the PowerShell extension.

Also:
PHP:
$groups = @(
    "Group1",
    "Group2",
    "Group3"
)

foreach ($g in $groups)
{
    Get-ADGroupMember $g | Select-Object Name | Out-File "C:\Temp\$g.txt"
}
And maybe consider just getting the groups dynamically if possible? Like you can just grab all the groups in a certain OU with Get-ADGroup.
 
Last edited:
Top