Found 463 Articles for PowerShell

How to use the ErrorAction parameter in PowerShell?

Chirag Nagrekar
Updated on 27-May-2020 09:32:39

8K+ Views

Like ErrorActionPreference variable, ErrorAction parameter works similarly. ErrorAction parameter supported in Advance functions and most of the built-in cmdlets in PowerShell. It is useful to convert the non-terminating error to the terminating error and then you can handle them with try/catch blocks.Supported Values and Examples, Continue − This is the default value of the ErrorAction parameter and Error will be displayed and commands listed in Pipeline will be executed further.Get-WmiObject -Class Win32_Logicaldisk -ComputerName Nonexist -ErrorAction Continue Write-Host "`nHello World" -BackgroundColor DarkGreenOutput Get-WmiObject : The RPC server is unavailable. At line:1 char:1 + Get-WmiObject -Class Win32_Logicaldisk -ComputerName Nonexist -ErrorA ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ... Read More

How to use the ErrorActionPreference variable in PowerShell?

Chirag Nagrekar
Updated on 27-May-2020 09:30:49

5K+ Views

ErrorActionPreference variable in PowerShell is to control the non-terminating errors by converting them to terminating errors. Error handling depends upon which value you assign to $ErrorActionPreference variable.The values are as below.Continue − This is the default value of the variable and when the error occurs, an error is displayed in the PowerShell console, and the script continues the execution.Get-WmiObject -Class Win32_Logicaldisk -ComputerName Nonexist Write-Host "Hello World"Output Get-WmiObject -Class Win32_Logicaldisk -ComputerName Nonexist Write-Host "Hello World" Get-WmiObject : The RPC server is unavailable. At line:2 char:1 + Get-WmiObject -Class Win32_Logicaldisk -ComputerName Nonexist + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException + FullyQualifiedErrorId ... Read More

What is terminating and non-terminating errors in PowerShell?

Chirag Nagrekar
Updated on 27-May-2020 09:27:29

1K+ Views

Powershell generates two types of errors when it executes script or commands. Terminating errors and Non-Terminating errors.Terminating error − This error generated by script, functions, or commands you create and it stops or halts the execution of the script so that the commands in the next lines can’t be executed. To handle this error proper mechanism is needed otherwise there would be an error message displayed.For example, PS C:\WINDOWS\system32>> This-commandnotexist This-commandnotexist : The term 'This-commandnotexist' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path ... Read More

What is use of $error variable in PowerShell?

Chirag Nagrekar
Updated on 27-May-2020 09:25:09

3K+ Views

Error variable in PowerShell is to view the errors generated in the current PowerShell session. We can say that the $Error variable is the container that stores all the errors and the latest error will be displayed first. For the example below, we will set the $Errorview to Category view to minimizing the error display content. By default $ErrorView is a Normal view.$ErrorView = "Categoryview"Now we will see the $error variable example, PS C:\WINDOWS\system32> asdds ObjectNotFound: (asdds:String) [], CommandNotFoundException PS C:\WINDOWS\system32> Get-process asddsd ObjectNotFound: (asddsd:String) [Get-Process], ProcessCommandExceptionHere, there is one wrong command and one wrong input we have written so ... Read More

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

309 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

6K+ 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

2K+ 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

7K+ 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

2K+ 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

Advertisements