Share via


Comparison Operators in PowerShell

Comparison Operators are key players in Automation. Any reporting requires manipulations. Most office managers demand a User Report with some additional values or something that needs to be removed. Requirement: We are planning to manipulate reports based on the day. We do have multiple functions so it should be called based on the day.

  • Monday - Do Task 1
  • Tuesday - Do Task 2
  • Wednesday - Do Task 3
  • Thursday - Do Task 4
  • Friday - Do Task 5

Solution : Base logic using PowerShell.

$dayofweek = (Get-Date).DayOfWeek
$dayofweek

We can get Day of Week in a variable and process our commands as required. You can find the dirty PowerShell code below by using if and comparison operator "-Match":

$DayofWeek = (Get-Date).DayOfWeek
if($DayofWeek -Match "Mon")
{Write-Host "Do Something here" -ForegroundColor Yellow}
if($DayofWeek -Match "Tue")
{Write-Host "Chen V...Because I match with 'Tue'sday" -ForegroundColor Green}
if($DayofWeek -Match "Wed")
{Write-Host "Do Something here" -ForegroundColor Red}
if($DayofWeek -Match "Thu")
{Write-Host "Do Something here" -ForegroundColor Black}
if($DayofWeek -Match "Fri")
{Write-Host "Do Something here" -ForegroundColor DarkBlue}

So far so good for the base work. But the LYNC report has SIP IDs as "SIP:". Identity has CN=UserName plus some other OU details. We need a flat file with no "SIP:" character. This will be given as an input file to other applications.

Now comes the importance of -Replace operator:

Import-Csv C:\Temp\LYNC.CSV | %{S_.SIPAddress -Replace "SIP:" , ""} | Out-File C:\Temp\LYNC.txt

help about_Comparison_Operator