Powershell Tip #145: Select-String cmdlet with case sensitive search
Tip: You can use case sensitive with the Select-String cmdlet.
1 2 |
# Case insensitive Get-Content -Path .\File1.txt | Select-String -Pattern 'Hello' -CaseSensitive |
Tip: You can use case sensitive with the Select-String cmdlet.
1 2 |
# Case insensitive Get-Content -Path .\File1.txt | Select-String -Pattern 'Hello' -CaseSensitive |
Tip: You can compare two strings case sensitive
1 2 3 4 5 |
# Operator -ceq 'Hello' -ceq 'HELLO' # Compare method [string]::Compare('Hello', 'HELLO', $false) |
The overload definitions for the compare method : String.Compare Method https://docs.microsoft.com/en-us/dotnet/api/system.string.compare
Tip: You can get the UUID (Universally Unique Identifier) of one local or remote computer.
1 2 3 4 5 6 7 |
# Local (Get-WmiObject -Class Win32_ComputerSystemProduct).UUID (Get-CimInstance -Class Win32_ComputerSystemProduct).UUID # Remote (Get-WmiObject -Class Win32_ComputerSystemProduct -ComputerName SERVER1).UUID (Get-CimInstance -Class Win32_ComputerSystemProduct -ComputerName SERVER1).UUID |
Tip: You can contenate two text files in PowerShell. Here there is one folder with 3 files. Concatenate two files (File1 + File2) and save to the file File1andFile2.txt You can concatenate three files having different prefix names and save to the file All.txt Note: You can also use the -Encoding parameter
Tip: You can swap (mutually exchanging their values) two variables.
1 2 3 4 5 |
$a = "a" $b = "b" # Swap $a, $b = $b, $a |
Tip: You can convert a string to title case (every word start with a capital letter).
1 2 3 |
$user = 'JOHN SMITH' $textInfo = (Get-Culture).TextInfo $textInfo.ToTitleCase($user.ToLower()) |
Note: You have to convert the string to lower case (if not already) to ensure all words are converted in title case.
Tip: You can replace every occurence of a string in a file.
1 2 3 4 5 |
# Preview (Get-Content -Path C:\Demo\test.txt).Replace('12345', '56789') # Save (to overwrite the file) (Get-Content -Path C:\Demo\test.txt).Replace('56789', '12345') | Set-Content -Path C:\Demo\test.txt |
Tip: You determine the location of the current script.
1 2 3 4 5 |
# PowerShell 3+ $PSScriptRoot # PowerShell 2 Split-Path -Parent $MyInvocation.MyCommand.Definition |
Tip: You can convert a number (64) to binary (1000000).
1 2 3 4 5 |
# Decimal > Binary [Convert]::ToString(64,2) # Binary > Decimal [Convert]::ToInt32('1000000',2) |
You can see the overload defintions of the ToString method from the Convert class:
Tip: You can read the first and last byte of a file.
1 2 3 4 5 6 7 |
$byte = [System.IO.File]::ReadAllBytes('C:\Demo\test.txt') # First byte $byte[0] # Last byte $byte[-1] |