# Confused about text files

**URL:** <https://forums.powershell.org/t/confused-about-text-files/7731>\
**Category:** PowerShell Help\
**Created:** [December 16, 2016, 3:12pm UTC](https://forums.powershell.org/t/confused-about-text-files/7731 "2016-12-16T15:12:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tony-antony](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tony-antony/32/192_2.png) [@tony-antony](https://forums.powershell.org/u/tony-antony)\
**Post date:** [December 16, 2016, 3:12pm UTC](https://forums.powershell.org/t/confused-about-text-files/7731/1 "2016-12-16T15:12:11Z")

</div>

Hello,

I need help, I know I asked this before, but it doesn’t make any sense.

I tried putting everything inside the while loop, but I get the same result, so I have everything outside the while loop now.

I’m sure its something simple, but I can’t figure it out.

Issue 1: Line 14, it displays the users with the [one@one.com](mailto:one@one.com) I added, this is good, but when I do a .count, it gives me 8 instead of 9 which is the correct count.

[1=mkelley@company.com](mailto:1=mkelley@company.com)  
[2=jbruce@company.com](mailto:2=jbruce@company.com)  
[3=oreynolds@company.com](mailto:3=oreynolds@company.com)  
[1438=alippard@company.com](mailto:1438=alippard@company.com)  
[1439=klaw@company.com](mailto:1439=klaw@company.com)  
[1440=dmarsh@company.com](mailto:1440=dmarsh@company.com)  
[1441=jonathanj@company.com](mailto:1441=jonathanj@company.com)  
[1442=aralls@company.com](mailto:1442=aralls@company.com)  
[one@one.com](mailto:one@one.com)

```
$INI = '\\hqfs1\users\tantony\PowerShell\CalenderGroup\config.ini'
$Read_INI = Get-Content $INI
$Users_Line_Num = ($Read_INI | Select-String -CaseSensitive "[Users]" -SimpleMatch).LineNumber
$Membersof_Line_Num = ($Read_INI | Select-String -CaseSensitive "[MembersOf-1]" -SimpleMatch).LineNumber - 2
$Users = $Read_INI[$Users_Line_Num..$Membersof_Line_Num]

$Continue = $null

function Add_User
{
    $User = Read-Host "Enter the new email address to add"
    $Read_INI[$Membersof_Line_Num] += "`r`n$User"
    $Read_INI | Set-Content $INI
    $Read_INI[$Users_Line_Num..$Membersof_Line_Num]
}

While($Continue -ne 0)
{
    switch($Continue)
    {
        1{Add_User}
    }

    Write-Host "[1]`t`tAdd new user"
    Write-Host "[0]`t`tExit menu"

    $Continue = Read-Host "`nPlease select an option and press ENTER"
}
```

Issue 2:

Line 14, I have $Users, which is $Read\_INI[$Users\_Line\_Num…$Membersof\_Line\_Num], but it doesn’t show [one@one.com](mailto:one@one.com), and when I do a .count, it gives me 8 instead of 9 which is the correct count.

[1=mkelley@company.com](mailto:1=mkelley@company.com)  
[2=jbruce@company.com](mailto:2=jbruce@company.com)  
[3=oreynolds@company.com](mailto:3=oreynolds@company.com)  
[1438=alippard@company.com](mailto:1438=alippard@company.com)  
[1439=klaw@company.com](mailto:1439=klaw@company.com)  
[1440=dmarsh@company.com](mailto:1440=dmarsh@company.com)  
[1441=jonathanj@company.com](mailto:1441=jonathanj@company.com)  
[1442=aralls@company.com](mailto:1442=aralls@company.com)

```
$INI = '\\hqfs1\users\tantony\PowerShell\CalenderGroup\config.ini'
$Read_INI = Get-Content $INI
$Users_Line_Num = ($Read_INI | Select-String -CaseSensitive "[Users]" -SimpleMatch).LineNumber
$Membersof_Line_Num = ($Read_INI | Select-String -CaseSensitive "[MembersOf-1]" -SimpleMatch).LineNumber - 2
$Users = $Read_INI[$Users_Line_Num..$Membersof_Line_Num]

$Continue = $null

function Add_User
{
    $User = Read-Host "Enter the new email address to add"
    $Read_INI[$Membersof_Line_Num] += "`r`n$User"
    $Read_INI | Set-Content $INI
    $Users
}

While($Continue -ne 0)
{
    switch($Continue)
    {
        1{Add_User}
    }

    Write-Host "[1]`t`tAdd new user"
    Write-Host "[0]`t`tExit menu"

    $Continue = Read-Host "`nPlease select an option and press ENTER"
}
```

Thank you,

Tony

---

<div class="post-metadata">

**Author:** ![jonathan-warnken](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jonathan-warnken/32/469_2.png) [@jonathan-warnken](https://forums.powershell.org/u/jonathan-warnken)\
**Post date:** [December 16, 2016, 3:39pm UTC](https://forums.powershell.org/t/confused-about-text-files/7731/2 "2016-12-16T15:39:53Z")

</div>

You need to read the file again after you make your update  
try this

```
$Continue = $null

function Add_User
{
    $INI = '\\hqfs1\users\tantony\PowerShell\CalenderGroup\config.ini'
    $Read_INI = Get-Content $INI
    $Users_Line_Num = ($Read_INI | Select-String -CaseSensitive "[Users]" -SimpleMatch).LineNumber
    $Membersof_Line_Num = ($Read_INI | Select-String -CaseSensitive "[MembersOf-1]" -SimpleMatch).LineNumber - 2
    $Users = $Read_INI[$Users_Line_Num..$Membersof_Line_Num]
    $User = Read-Host "Enter the new email address to add"
    $Read_INI[$Membersof_Line_Num] += "`r`n$User"
    $Read_INI | Set-Content $INI
    $Read_INI = Get-Content $INI
    $Users_Line_Num = ($Read_INI | Select-String -CaseSensitive "[Users]" -SimpleMatch).LineNumber
    $Membersof_Line_Num = ($Read_INI | Select-String -CaseSensitive "[MembersOf-1]" -SimpleMatch).LineNumber - 2
    $Users = $Read_INI[$Users_Line_Num..$Membersof_Line_Num]
    $Read_INI[$Users_Line_Num..$Membersof_Line_Num]
}

While($Continue -ne 0)
{
    switch($Continue)
    {
        1{Add_User}
    }

    Write-Host "[1]`t`tAdd new user"
    Write-Host "[0]`t`tExit menu"

    $Continue = Read-Host "`nPlease select an option and press ENTER"
}
```

---

<div class="post-metadata">

**Author:** ![tony-antony](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tony-antony/32/192_2.png) [@tony-antony](https://forums.powershell.org/u/tony-antony)\
**Post date:** [December 16, 2016, 3:52pm UTC](https://forums.powershell.org/t/confused-about-text-files/7731/3 "2016-12-16T15:52:25Z")

</div>

Thank you Jonathan I’ll try that, one more question.

The reason I set my variables outside of loop is because I have to use the same variables again later such as when I delete a user.

Is there a way to make the variables “public”? Because right now, the variables can only be used within the Add\_User function correct?

---

<div class="post-metadata">

**Author:** ![jonathan-warnken](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jonathan-warnken/32/469_2.png) [@jonathan-warnken](https://forums.powershell.org/u/jonathan-warnken)\
**Post date:** [December 16, 2016, 4:03pm UTC](https://forums.powershell.org/t/confused-about-text-files/7731/4 "2016-12-16T16:03:23Z")

</div>

You can set some of them like the ini path out side of the function like you had. A better option would be to use a function to read the file and return the properties that you want so you reduce the code repetition

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:39pm UTC](https://forums.powershell.org/t/confused-about-text-files/7731/5 "2024-05-16T20:39:44Z")

</div>


