Author: Arpit Mandliya
- 21 April
PowerShell Check if Directory is Empty
For this article, we are using three directories, E:\Test\Script Files, E:\Test\Test Files, and E:\Test\Sample Files. The E:\Test\Script Files directory is not empty and contains test.bat and testPS.ps1 files, while the E:\Test\Test Files directory is empty because it contains nothing, neither files nor folders The E:\Test\Sample Files directory is also not empty; it contains file.txt, file1.txt, […]
- 21 April
Check if PowerShell is Running as Admin
Using WindowsPrincipal and WindowsIdentity Classes Use the WindowsPrincipal and WindowsIdentity Classes to check if PowerShell runs as an admin. [crayon-67689ee2d4b91140533154/] [crayon-67689ee2d4b96163716050/] The above code returned PowerShell is running as admin if the PowerShell is running as an administrator; otherwise, it returned PowerShell is not running as admin. Let’s break down the code to understand it. […]
- 18 April
PowerShell Check If Software Is Installed
Using Get-Package with Select-Object Cmdlet Use Get-Package with the Select-Object cmdlet to check if the specified software is installed. [crayon-67689ee2d4d51295972195/] [crayon-67689ee2d4d55331584875/] First, we declared and initialized a variable called $software having a software’s complete name that we wanted to check. Next, we used the Get-Package cmdlet to retrieve a list of all packages installed on […]
- 16 April
Convert Array to ArrayList in PowerShell
One of the everyday tasks is converting an array to an ArrayList in PowerShell. It can be helpful when you need to work with dynamic data structures or manipulate data in memory. However, before we dive into how to convert an array to ArrayList, it’s essential to understand the difference between these two. An array […]
- 16 April
Bash Split String and Get Last Element
Using read Command Use the read command with parameter expansion to split the string and get the last element in Bash. [crayon-67689ee2d51d8816317824/] [crayon-67689ee2d51dd302671327/] In the above example, the read command is used with the ${myString##*:} parameter expansion to get the last element of the string which is then assigned to the lastElement variable using the […]
- 16 April
Check if URL is Reachable in PowerShell
Using Test-Connection Cmdlet To check if URL is reachable in PowerShell: Convert url to System.url object and get host property using Select-Object cmdlet. Use Test-Connection cmd to check if host is reachable or not. [crayon-67689ee2d5503442590684/] [crayon-67689ee2d5507641355429/] In the above code, the Test-Connection cmdlet sends an ICMP echo request to the hostname. First, the code sets […]
- 16 April
Get Number from String in PowerShell
1. Introduction PowerShell, a versatile scripting language used primarily on Windows, often requires the extraction of numbers from strings for data processing and manipulation. For example, given a string Order1234, the goal is to extract the number 1234 from it. The expected output, in this case, is a numerical value (integer or float) extracted from […]
- 16 April
Convert String to Byte Array in PowerShell
Using System.Text.Encoding Class The System.Text.Encoding class converts the specified string to a byte array in PowerShell. This class can use a particular, default, or custom encoding format. Let’s see how we can use them. Use System.Text.Encoding Class with a Particular Encoding Use the System.Text.Encoding class with specific encoding to convert a string to byte array […]
- 16 April
Format Number to 2 Decimal Places in PowerShell
In PowerShell, there are so many methods to round or format the numbers to two decimal points. In the following methods, the following steps will be followed: Initialize a variable with the floating point number Round or format the number using the specific method Display the Rounded or formatted number on the screen Using ToString() […]