Hello,
I have an INI files which contains some email addresses. Among them, I have tantony@company.com and tony@company.com
How would I modify the below code so that it’s “case sensitive”? I have it selected, but if I look up tony@company.com, it also finds tantony@company.com
$Del_User = $Read_INI | Select-String -CaseSensitive "$Del_User" -SimpleMatch
This is the current output I’m getting, notice it’s showing 2 emails. I only want it to search for tony@company.com
Please select an option and press ENTER: 2
Enter the email you want to delete: tony@company.com
Deleted tantony@company.com tony@company.com
Thank you,
Tony
Case sensitivity relates to UPPERCASE or lowercase. You have all lowercase for the e-mail addresses so that’s not relevant here. The problem is that your pattern tony@company.com matches both the exact string tony@company.com and the substring tantony@company.com.
What you need to do is modify the pattern so that it only returns exact matches. Try using the start and end anchors to get an exact match “^Del_user$”.
PS E:\> $del_user = 'tony@company.com'
PS E:\> 'tony@company.com' -match $del_user
True
PS E:\> 'tantony@company.com' -match $del_user
True
PS E:\> 'tantony@company.com' -match "^$del_user$"
False
PS E:\>
Get-Help about_regular_expressions for more.
Thank you, I figured it was a regex, but I couldn’t figure that out.
is this correct?
$Del_User = $Read_INI | Select-String -AllMatches "^$Del_User" -SimpleMatch
No, it should be:
$Del_User = $Read_INI | Select-String -AllMatches "^Del_User$" -SimpleMatch
Note: this will work for a list of e-mail addresses but you’ll need a more complex expression for a document that contains other data.
When I add that, it’s not showing tantony@company.com or tony@company.com
$Del_User = $Read_INI | Select-String -AllMatches "^Del_User$" -SimpleMatch
It looks like this, it should say Deleted tony@company.com
Please select an option and press ENTER: 2
Enter the email you want to delete: tony@company.com
Deleted
I’ll try again 
$Del_User = $Read_INI | Select-String -AllMatches "^$Del_User$"
I owed you a dollar. -SimpleMatch doesn’t use regular expressions so should be omitted.
I’m sorry that didn’t work either. I’ll post my whole code here (I know it’s not the best formatted), I’m having issues with line 28. This is my original line, line 29 is your code.
I have other issues also, but this is causing most of the problem.
$Continue = $null
$INI = '\\hqfs1\users\tantony\PowerShell\CalenderGroup\config.ini'
function Add_User
{
$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 = $Read_INI | Where-Object {$_ -ne $User} | Set-Content $INI -Force
$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]
Write-Host "Added" $User -ForegroundColor Green
Reindex
}
function Delete_User
{
$Read_INI = Get-Content $INI
$Del_User = Read-Host "Enter the email you want to delete"
#$Del_User = $Read_INI | Select-String -CaseSensitive "$Del_User" -SimpleMatch
$Del_User = $Read_INI | Select-String -AllMatches "^$Del_User$"
#$Del_User = $Read_INI | Select-String -AllMatches "^$Del_User$"
$Read_INI | where {$_ -ne $Del_User} | Set-Content $INI -Force
$Del_User = $Del_User -creplace '^[^=]*=', ''
Write-Host "Deleted" $Del_User -ForegroundColor Red
Reindex
}
function Add_To_Calender
{
$Read_INI = Get-Content $INI
$Calenders= $Read_INI | Select-String -CaseSensitive "MembersOf" -SimpleMatch
$GroupCalendars= ($Read_INI | Select-String -CaseSensitive "[GroupCalendars]" -SimpleMatch).LineNumber
$Users= ($Read_INI | Select-String -CaseSensitive "[Users]" -SimpleMatch).LineNumber - 2
$Calender_List = $Read_INI[$GroupCalendars..$Users]
$Calender_Index = $Read_INI | Select-String -CaseSensitive "[MembersOf" -SimpleMatch
$Calender_List
$Get_Calender = Read-Host "Which calender number do you want to add user to?"
$Get_User = Read-Host "Which email do you want to add to" $Calender_List[$Get_Calender-1]
$Calender_Index | Select-Object -Index 2
Write-Host "Added" $Get_User "to" $Calender_List[$Get_Calender-1] -ForegroundColor Green
}
function Reindex
{
$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]
$Users_Line_Num = ($Read_INI | Select-String -CaseSensitive "[Users]" -SimpleMatch).LineNumber - 1
For($Index=0; $Index -lt $Users.length; $Index++)
{
$Index_Plus_One = $Index + 1;
$Index_Minus_One = $Index - 1;
if(($Users[$Index].Contains("@") -eq $false))
{
$Read_INI = $Read_INI | Where-Object {$_ -ne $Users[$Index]} | Set-Content $INI -Force
$Users
}
if(($Users[$Index]).Contains("=") -eq $true)
{
$Email = ($Users[$Index]) -creplace '^[^=]*=', ''
}
else
{
$Email = $User
}
#$Re_Index = "$Index_Plus_One" + "=" + "$Email"
#$Read_INI = $Read_INI | Where-Object {$_ -ne $Users[$Index]}
#$Read_INI[$Users_Line_Num] += "`r`n$Re_Index"
#$Read_INI | Set-Content $INI -Force
}
$Re_Index = "$Index_Plus_One" + "=" + "$Email"
$Read_INI = $Read_INI | Where-Object {$_ -ne $Users[$Index]}
#$Read_INI[$Membersof_Line_Num] += "`r`n$Re_Index"
$Read_INI[$Membersof_Line_Num] = $Re_Index
$Read_INI | Set-Content $INI -Force
}
function Find_User_Index ([string]$email)
{
$E = ($Read_INI | Select-String -AllMatches "$email" -SimpleMatch)
$E.ToString().Trim("=$email")
}
While($Continue -ne 0)
{
switch($Continue)
{
1{Add_User}
2{Delete_User}
3{Add_To_Calender}
default {"Please choose correct manu option!"}
}
Write-Host "[1]`tAdd new user"
Write-Host "[2]`tDelete user"
Write-Host "[3]`tAdd user to calender group"
Write-Host "[0]`tExit menu"
$Continue = Read-Host "`nPlease select an option and press ENTER"
}
Thank you,
Can you post example data for the ini file as well please. The line I posted in my last post works fine when tested with a basic list of e-mail addresses. I’m guessing your INI file is more complex?
btw, if you email list contains both
jim.morrison@mail.com
jim_morrison@mail.com
you delete both jims if enter ‘jim.morrison@mail.com’
‘.’ - regex symbol
OK, so you have some digits and an equals sign before the e-mail address. This should work for the search:
$Del_User = $Read_INI | Select-String -AllMatches "^\d+=$Del_User$"
The plain English regex is ‘search for a line starting with (^) one or more digits (\d+) followed by an equals sign, followed by the e-mail address’.
I added our previous test user to the file so the output at this point would be, for example, 7=tony@company.com
If you just want the e-mail address, split on the = and select the second element:
$Del_User = (($Read_INI | Select-String -AllMatches "^\d+=$Del_User$") -split '=')[1]
Thank you, I’ll try that.
Thank you,
I tested it with tantony and tony and it works
$Del_User = $Read_INI | Select-String -AllMatches "^\d+=$Del_User$"