Hello All,
I have a text file containing the following:
*---- A D G R O U P S ----*
AD Group001
AD Group002
AD Group003
AD Group004
AD Group005
AD Group006
AD Group007
AD Group008
AD Group009
AD Group010
*---- S C R I P T N O T E S ----*
IT Service Desk ticket number:
TEST000006
Account AD groups were copied from:
IleneDover
I’m trying to create a script that will just display the listed AD groups. The script kinda works, and displays the following:
*---- A D G R O U P S ----*
AD Group001
AD Group002
AD Group003
AD Group004
AD Group005
AD Group006
AD Group007
AD Group008
AD Group009
AD Group010
But I just want the AD groups and not the heading “---- AD GROUPS ----” to be displayed. I think this should be a simple fix, and I usually can muddle my way through these things but my “muddle isn’t muddling” today. Any help would be greatly appreciated. Thanks!
Here’s my code:
$filePath = "C:\_ScriptOutput\TEST-ADGroupList.txt"
$startString = "*---- A D G R O U P S ----*"
$stopString = "*---- S C R I P T N O T E S ----*"
$reading = $false
Get-Content -Path $filePath | ForEach-Object {
$line = $_
# Exit if the stop condition is met
if ($line -eq $stopString)
{
break
}
if ($line -like "*$startString*") {
$reading = $true
}
# Start listing line items when the start string is found.
if ($reading) {
Write-Host $line
}
}
Hi and welcome to the forum. Thanks for providing good examples and complete code.
In testing, it looks like you could get away with adding the word “return” after you match on the start string
$filePath = "C:\_ScriptOutput\TEST-ADGroupList.txt"
$startString = "*---- A D G R O U P S ----*"
$stopString = "*---- S C R I P T N O T E S ----*"
$reading = $false
Get-Content -Path $filePath | ForEach-Object {
$line = $_
# Exit if the stop condition is met
if ($line -eq $stopString)
{
break
}
if ($line -like "*$startString*") {
$reading = $true
return
}
# Start listing line items when the start string is found.
if ($reading) {
Write-Host $line
}
}
Continue doesn’t work in Foreach-Object the way it does in a Foreach loop, but return has the right effect. Running the above yields
AD Group001
AD Group002
AD Group003
AD Group004
AD Group005
AD Group006
AD Group007
AD Group008
AD Group009
AD Group010
Or, if we did it with a Foreach loop
foreach ($Line in (Get-Content $filePath)) {
# Exit if the stop condition is met
if ($line -eq $stopString)
{
break
}
if ($line -like "*$startString*") {
$reading = $true
continue
}
# Start listing line items when the start string is found.
if ($reading) {
Write-Host $line
}
}
Here the use of continue works by skipping the rest of the code in the loop and continuing to the next object in the loop. Since you were already renaming the current object in the pipeline to $line anyway this seems like a better approach.
I would recommend switch -File, it reads file line by line while get-content would read the whole file before iteration which might be consuming when the file is large.
$filePath = 'C:\_ScriptOutput\TEST-ADGroupList.txt'
$startString = '*---- A D G R O U P S ----*'
$stopString = '*---- S C R I P T N O T E S ----*'
$reading = $false
switch -Wildcard -File $filePath {
"$stopString" {
break
}
"*$startString*" {
$reading = $true
}
default {
if ($reading) {
$_
}
}
}