PowerShell
13 MarchWait for Command to Finish in PowerShell
Using Start-Process Cmdlet Use Start-Process with the -Wait parameter to wait for the command to finish. [crayon-6904fec04216b835931699/] OUTPUT: In the above output, we can see that the command waits until the file explorer is opened. To do that, we used Start-Process with the -Wait parameter. The Start-Process cmdlet is used to start one or multiple […]
13 MarchCheck if Array Contains Element in PowerShell
1. Overview In this article, we will see different ways to check if array contains element in PowerShell using -contains operator, Contains() method, Where-Object cmdlet, -in operator, indexOf() method, and LINQ. 2. Introduction to Problem Statement We are given an array and element. Our goal is to check if array contains the element. For instance, […]
13 MarchDownload File from URL in PowerShell
Using Invoke-WebRequest Cmdlet Use the Invoke-WebRequest cmdlet to download the specified file from the specified URL to the local path in your machine. [crayon-6904fec042bf4589895590/] First, we created two variables named $URL and $LocalPath containing a URL and the local path of your machine. Note that variables are prefixed with the $ sign in PowerShell. Next, […]
11 MarchDelete Files Older than X Days in PowerShell
Delete Files Older than X Days in PowerShell Use Remove-Item cmdlet with the Get-ChildItem cmdlet and Where-Object cmdlet to delete files older than X days. [crayon-6904fec042d12008081549/] The above script retrieved all the items from the given path, filtered them based on the specified property and kept only those files/folders that were less than the given […]
10 MarchPing List of Computers in PowerShell
Different Ways to Ping a List of Computers in PowerShell There are multiple ways that we can use to ping a list of computers in PowerShell. Now, what does ping means? Pinging a computer means determining if the networked computer is available and responsive. If the networked computer (we can also call it a target […]
10 MarchPowerShell Array to String
Using Double Quotation Marks Use double quotation marks to convert array object to string in PowerShell. [crayon-6904fec0438e3175559324/] [crayon-6904fec0438e8721754121/] First, we created an array object and saved it in the $arrayObject variable; we can also create an array object using the array operator (@()), for instance, $arrayObject = @("How", "are", "you?"). Next, we enclosed the $arrayObject […]
08 MarchReturn Value from Function in PowerShell
Using the return Keyword PowerShell allows us to use the return keyword to return single or multiple values from a function. Let’s learn both of the scenarios. Use the return Keyword to Return a Single Value To return a single value from a particular function: Create a function that returns something, a string, a number […]
08 MarchCheck if String is Null or Empty in PowerShell
Using -not and -eq Operators Use the -not operator to check if the string is null or empty in PowerShell. [crayon-6904fec044128975094659/] [crayon-6904fec04412d197583578/] First, we declared the $string variable and initialized it with $null. What is $null? It is an automatic variable in PowerShell representing the NULL value. Now, what is NULL? We can think of […]
08 MarchConvert String to Int in PowerShell
Using Dynamic Casting Use dynamic casting to convert the given string to int in PowerShell. [crayon-6904fec04468a371536520/] [crayon-6904fec04468f736731105/] First, we created and initialized a $string variable; then, we specified the type as [int] before the string variable to do dynamic casting. In dynamic casting, we tell the compiler to think about things differently. To cross-check, we […]