Found 975 Articles for Software & Coding

What is the use of $Lastexitcode and $? Variable in PowerShell?

Chirag Nagrekar
Updated on 27-May-2020 09:24:41

6K+ Views

$LastExitCode in Powershell is the number that represents the exit code/error level of the last script or application executed and $? (Dollar hook) also represents the success or the failure of the last command. In general, both represent the same but the output method is different. The first command output is in the Number format (0 and 1) and the latter command is for Boolean (True or False) output.For example, PS C:\WINDOWS\system32> $LASTEXITCODE 0 PS C:\WINDOWS\system32> $? TrueAs you see the output, 0 represents the success status of the $LastExitCode command and $True for the $?.Now if the command doesn’t ... Read More

Explain secure password Encryption with PowerShell.

Chirag Nagrekar
Updated on 16-May-2020 10:59:37

447 Views

Many times we need to use passwords in PowerShell and need to pass it to the credential parameter and a password should be always a secure string, not a plain text. There are few methods to encrypt the password as mentioned below.a) Get-Credential FormatWe have one method where we can store the username and password is through cmdlet Get-Credential. It will provide a GUI prompt. You can store this password into a variable and use it later in the command.$cred = Get-CredentialCredentials are stored into $cred variable. Here is the value of the variable. output below.PS C:\WINDOWS\system32> $cred UserName   ... Read More

What is the difference between –Match, -Like and –Contains Operator in PowerShell?

Chirag Nagrekar
Updated on 15-May-2020 13:05:42

8K+ Views

All the above 3 operators (Match, Like, and Contains) mentioned are the comparison operator in PowerShell. Match and Like operators are almost similar operator only difference is the Wildcard character and the Contains operator is entirely different. We will see the example and understand how they work.ExamplePS C:\WINDOWS\system32> "This is a PowerShell String" -Match "PowerShell" True PS C:\WINDOWS\system32> "This is a PowerShell String" -Like "PowerShell" False PS C:\WINDOWS\system32> "This is a PowerShell String" -Contains "PowerShell" FalseIf you see the output of the above example, the result is True only for the Match statement and reason is when you match the ... Read More

How to write Progress Bar in PowerShell?

Chirag Nagrekar
Updated on 15-May-2020 13:03:20

3K+ Views

When we write a script in PowerShell, users who execute it, expect to see the progress of the script instead of waiting idle and looking at the blank cursor while the script is executing the background task. One way to achieve is to use the Verbose parameter to see the progress but it doesn’t show the graphical progress. To see the graphical progress, you can use Write-Progress cmdlet supported by PowerShell.Write-Progress cmdlet mainly depends on 3 parameters.Activity − Title of the progress bar or you can mention the activity name that is being performed.Status − Subtitle of the Progress bar. ... Read More

How to remove empty string/lines from PowerShell?

Chirag Nagrekar
Updated on 15-May-2020 12:57:43

10K+ Views

In many instances, you need to remove empty lines or strings from the PowerShell string array or the files. In this article instead of removing empty string, we will get the result or filter the output from lines that are not empty. In this way, we can get the output without empty lines.Consider the example below, we have a file called EmptryString.txt and we need to remove empty lines from the content.The content of the text file is as below.PS C:\Windows\System32> Get-Content D:\Temp\EmptyString.txt This is example of empty string PowerShell PowerShell DSC String Array HelloYou just need to apply the ... Read More

How to get the port number of the processes using PowerShell?

Chirag Nagrekar
Updated on 04-May-2020 13:22:15

3K+ Views

When we use Get-Process cmdlet in PowerShell, it doesn’t have properties to get Port number the processes use. So here we will write a function that will provide us the ports number associated with the processes.There is one windows command NETSTAT which provides the Port number and the associated process ID but doesn’t provide the process name. We have Get-Process command which provides the process name and the PID (Process ID) so we can write a program that can associate both the commands and we can retrieve the process ID, local address, remote address, and if the state of the ... Read More

Explain ValueFromPipeline in PowerShell Advanced Functions.

Chirag Nagrekar
Updated on 04-May-2020 07:15:45

5K+ Views

Consider the below example, we have created Advanced function to get the specific process information like Process Name, Process ID (PID), Start Time, Responding status, etc.function Get-ProcessInformation{    [cmdletbinding()]    param(       [Parameter(Mandatory=$True)]       [string[]]$name    )    Begin{       Write-Verbose "Program started"    }    Process{       Write-Verbose "Extracting Process Inforamtion" Get-Process $name | Select Name, ID, StartTime, Responding | ft - AutoSize    }    End{       Write-Verbose "Function ends here"    } }Above is the advanced function example. When we run the above command, it will give ... Read More

Explain AllowEmptyString() and AllowEmptyCollection() in PowerShell Advanced Function.

Chirag Nagrekar
Updated on 04-May-2020 07:04:15

4K+ Views

AllowEmptyString() attribute works with the string variable and AllowEmptyCollection() work with the array of different data types (Collection).Consider the example below. Here, we are using a mandatory variable $name which is a string and $stringarray which is a string array.function print_String{    [cmdletbinding()]    param(       [parameter(Mandatory=$True)]       [string]$name,    )    Write-Output "Writing a single string"    $name }If we get the output of the above variable it will generate an error below.PS C:\WINDOWS\system32> print_String cmdlet print_String at command pipeline position 1 Supply values for the following parameters: name: print_String : Cannot bind argument to ... Read More

Explain the Mandatory Attribute in PowerShell Advanced Function.

Chirag Nagrekar
Updated on 04-May-2020 07:01:04

447 Views

We have an example of PowerShell advanced function below and we will try to understand how mandatory parameter works.function math_Operation{    [cmdletbinding()]    param([int]$val1, [int]$val2)    Write-Host "Multiply : $($val1*$val2)"    Write-Host "Addition : $($val1+$val2)"    Write-Host "Subtraction : $($val1-$val2)"    Write-Host "Divide : $($val1+$val2)" }When you execute the above example and don’t supply values then the script won’t ask you for the values, by default it will take the values and execute the program. See the execution below.PS C:\WINDOWS\system32> math_Operation Multiply : 0 Addition : 0 Subtraction : 0 Divide : 0Even if you have mentioned two variables ($val1, ... Read More

Explain the PowerShell Advanced Function.

Chirag Nagrekar
Updated on 04-May-2020 06:56:58

720 Views

Before starting the Advance PowerShell function, assuming we know about the PowerShell function. You can check the explanation on the PowerShell function below.https://www.tutorialspoint.com/explain-the-powershell-functionHere, we will take the math function example that calculates the different types of operations. We already have a code with the simple function as shown below.function math_Operation{    param([int]$val1, [int]$val2)    Write-Host "Multiply : $($val1*$val2)"    Write-Host "Addition : $($val1+$val2)"    Write-Host "Subtraction : $($val1-$val2)"    Write-Host "Divide : $($val1+$val2)" }The above example is of the simple function. When you run the above code and run the function from the terminal and you can notice you will ... Read More

Advertisements