PowerShell
14 AprilPowerShell Check If File Contains String
Using Select-String Cmdlet Use the Select-String cmdlet to check if the specified file contains the given string in PowerShell. $path = "E:\Test\file1.txt" $string = "customers" $result = Select-String $path -Pattern $string if ($result -ne $null) { Write-Host "The specified text file contains the string '$string'." } else { Write-Host "The specified text file does not […]
14 AprilPowerShell Convert Epoch Time to DateTime
Before moving towards various solutions to convert the Epoch time to Datetime, it is essential to know the Epoch time. Epoch time, POSIX time or Unix time is the system for denoting a point in time to a single number. This single number is the number of seconds (not counting leap seconds) that have elapsed […]
14 AprilPowerShell Convert Guid to String
💡TL;DR Use the ToString() method on System.Guid to convert a GUID to a string in PowerShell. $guid = [System.Guid]::NewGuid() $guidString = $guid.ToString() Write-Host $guid.GetType() - $guid Write-Host $guidString.GetType() - $guidString System.Guid - 1acc6445-c9ed-4bee-a6f9-fcf36b0bb830 System.String - 1acc6445-c9ed-4bee-a6f9-fcf36b0bb830 Before diving into the solutions, let’s understand what GUID is. In PowerShell, a Globally Unique Identifier (GUID) is a […]
10 AprilGenerate Random String in PowerShell
Using [System.Guid] Class To generate a random string in PowerShell: Create a globally unique identifier using the NewGuid() method. Use the ToString() method to transform the GUID (created in the previous step) to String format. Use the Write-Host cmdlet to print the random string. $randomString = ([System.Guid]::NewGuid()).ToString() Write-Host $randomString 95ffbbf8-f721-4cf2-886d-dcdbfae00665 The New-Guid() is a static […]
10 AprilMandatory Parameters in PowerShell
Using Mandatory Attribute Use the Mandatory attribute to make parameters mandatory in PowerShell. function DisplayName { param( [Parameter(Mandatory=$true)] [string]$Name ) Write-Host $Name } DisplayName "John" John First, we defined a function named DisplayName preceded by the function keyword. Inside this function, we used the param keyword (also referred to as param() block), which was used […]
09 AprilGet Yesterday’s Date in PowerShell
Using Get-Date Command with AddDays() method Use the Get -Date command with AddDays() method to get yesterday’s date in PowerShell. Pass -1 to AddDays() to substract one day from current date and time (Get-Date).AddDays(-1).ToString('yyyy-MM-dd') 2023-03-13 The simplest way to get yesterday’s date in PowerShell is by using the Get-Date cmdlet with AddDays() method. For example, […]
09 AprilGet All Files in Directory Recursively in PowerShell
In PowerShell, the directory can be retrieved recursively using the Get-ChildItem cmdlet and the -Recurse parameter. Get-ChildItem -Path "D:\Content Writing" -Recurse Directory: D:\Content Writing Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 09-04-2023 16:57 test1 d----- 09-04-2023 16:58 test2 Directory: D:\Content Writing\test1 Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 09-04-2023 16:57 7 […]
07 AprilPowerShell- Get Full Path of File
Get Full Path of File in PowerShell To get full path of file in powershell: Go to folder where file is present. You can go to any parent folder as well. Use Get-ChildItem with -Recurse option. Pipe it to where-object cmdlet to filter filename. In this example, we have given filename as test.ps1 Pipe it […]
07 AprilPowerShell – Get Permissions on Folder and Subfolders
To understand how to get permissions on a folder and subfolder in PowerShell, you must first understand what folder and subfolder permissions are. A folder’s permissions establish who can access and modify files and folders. A subfolder permission is a permission set for a folder containing other folders. In general, permissions set on a folder […]