PowerShell
- 14 April
PowerShell 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. [crayon-6790731112ffd491197047/] [crayon-6790731113004268247254/] First, we declared and initialized a variable named $path containing the location of the file1.txt file. Then, we created another variable called $string and set its value with "customers"; this is the value we […]
- 14 April
PowerShell 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 April
PowerShell Convert Guid to String
💡TL;DR Use the ToString() method on System.Guid to convert a GUID to a string in PowerShell. [crayon-67907311135a5296581997/] [crayon-67907311135a8142656719/] Before diving into the solutions, let’s understand what GUID is. In PowerShell, a Globally Unique Identifier (GUID) is a 128-bit identifier used to identify objects uniquely—for instance, folders, files, registry keys, and other system resources. Remember, GUIDs […]
- 10 April
Generate 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. [crayon-6790731113725071980029/] [crayon-6790731113728621263171/] The New-Guid() is a static method of [System.Guid] class […]
- 10 April
Mandatory Parameters in PowerShell
Using Mandatory Attribute Use the Mandatory attribute to make parameters mandatory in PowerShell. [crayon-6790731113940422640072/] [crayon-6790731113943054969360/] 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 to define the $Name parameter; this parameter was of the string […]
- 09 April
Get 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 [crayon-6790731113b9c827096576/] [crayon-6790731113b9f840549363/] The simplest way to get yesterday’s date in PowerShell is by using the Get-Date cmdlet with AddDays() method. For example, […]
- 09 April
Get All Files in Directory Recursively in PowerShell
In PowerShell, the directory can be retrieved recursively using the Get-ChildItem cmdlet and the -Recurse parameter. [crayon-6790731113c82604617099/] [crayon-6790731113c84296224121/] If you are looking to get all files in current directory in PowerShell, you can use below command: [crayon-6790731113c85835491286/] The folders and files can also be excluded using the -Exclude parameter. First, have a look at the […]
- 07 April
PowerShell- 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 April
PowerShell – 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 […]