Found 2039 Articles for Microsoft Technologies

How to restart a remote system using PowerShell?

Chirag Nagrekar
Updated on 24-Jul-2020 10:30:17

3K+ Views

To restart the remote computer, you need to use the Restart-Computer command provided by the computer name. For example, Restart-Computer -ComputerName Test1-Win2k12The above command will restart computer Test1-Win2k12 automatically and if you have multiple remote computers to restart then you can provide multiple computers separated with comma (, ).For example, Restart-Computer -ComputerName Test1-Win2k12, Test2-Win2k12Restart signal will be sent to both the computers at a time in the above example.You can also use the Pipeline to restart remote computers. For example, "Test1-Win2k12", "Test2-Win2k12" | Restart-Computer -VerboseOR(Get-Content C:\Servers.txt) | Restart-Computer -Verbose Few servers have dependencies on the other servers so main servers ... Read More

How to generate a strong password using PowerShell?

Chirag Nagrekar
Updated on 13-Jul-2020 05:39:25

559 Views

To generate a strong password with the PowerShell, we can use 8 or more characters password. In the below example, we are going to use the 8 character password, you can change the number of characters as per your convenience.This script you can bind into windows forms and provide it to the IT helpdesk to set the password for new users.In this example, we will use the Random integers between 33 and 126 and convert it to the Character, considering them they are the ASCII values for characters.You can find more about ASCII values in the table provided in the ... Read More

How to copy Items to the destination path with the differentcredentials in PowerShell?

Chirag Nagrekar
Updated on 13-Jul-2020 05:37:34

2K+ Views

To copy the items with the different credentials you need to use the credential parameter in the Copy-Item cmdlet.For example,Copy-Item C:\temp\* -Destination \Remoteshare\c$\temp -Credential (Get- Credential)Or$creds = (Get-Credential) Copy-Item C:\temp\* -Destination \Remoteshare\c$\temp -Credential $creds

How to Zip / Unzip files or folders using PowerShell?

Chirag Nagrekar
Updated on 13-Jul-2020 05:36:01

2K+ Views

Now it is easy to ZIP or extract (unzip) the files or folders using PowerShell. PowerShell has added features of the Archive module (Microsoft.PowerShell.Archive) from PowerShell 5.1 version.If you have PowerShell older version (version 4.0 or less) then you can download and install the module from the website or through the command line. However, only download and installing or manually copying the Archive module to the Module folder won’t work because it needs some dependent DLL files which are provided by .Net framework 4.5 so you need to consider upgrading .Net framework as well if it is below the 4.5 ... Read More

How to create a User Menu in PowerShell?

Chirag Nagrekar
Updated on 03-Jul-2020 08:26:54

4K+ Views

When you are writing a script and if you want to provide the user to select one option among multiple values and based on it to execute the command, we can generally use the Switch command and for it, we will ask the user choice in the below script.There is another .Net method to create a user menu, we will see it after the example from the description mentioned above.a. Switch command methodFor the Switch command-based user selection, we will first display the message for the user and then give him a choice for the selection through Read-Host command as ... Read More

How to get website SSL certificate validity dates with PowerShell?

Chirag Nagrekar
Updated on 03-Jul-2020 08:20:27

12K+ Views

SSL certificates are a very crucial part of the website. They play a key role in securing the exchange of information on both client and server sides by activating an HTTPS secure connection. In the below article with the PowerShell, we will get the certificate validity date (starting and expiry date) for the certificate using PowerShell.To achieve this, we need to make httpwebrequest but before that, we will ignore SSL warning by the below command.[Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }And then we wil make the HTTP web request by calling a .Net class.$url = "https://www.microsoft.com/" $req = [Net.HttpWebRequest]::Create($url)When we check the ... Read More

Explain Try/Catch/Finally block in PowerShell

Chirag Nagrekar
Updated on 06-Jun-2020 13:07:37

8K+ Views

Try/Catch block in PowerShell is to handle the errors which are produced in the script. To be specific, the errors should be terminating errors. The Finally block in the PowerShell is not mandatory to write each time along with Try/Catch but it will be executed regardless the error occurs or not.So when you use the Try block, the Catch block is mandatory but not Finally block.Try/Catch block with Terminating error − Below is the example of Terminating error without finally block.Exampletry{    This is not allowed    "This is Allowed" } catch{    Write-Host "Error occured" -BackgroundColor DarkRed }OutputPS C:\WINDOWS\system32> ... Read More

How to use the ErrorAction parameter in PowerShell?

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

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

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

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

Advertisements