Useful PowerShell Commands

Below is the list of PowerShell commands that you will find very useful if you are using PowerShell scripting to automate various tasks and if you are in DevOps.

  1. Get the registry property values using PowerShell script, for example below powershell command is useful to get the value of registry entries of Local Policies Security Options.

    Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa

  2. Update registry key value using PowerShell command.  Below registry key is correspond to  Configure the policy value for Computer Configuration -> Windows Settings -> Security Settings -> Local Policies -> Security Options -> “Network access: Do not allow storage of passwords and credentials for network authentication”.   If you are creating scheduled task and run under domain or service account (schtasks.exe /Create /RU ), you might get an error “A specified logon session does not exist.  It may already have been terminated.”  To resolve this error, you will have set below registry key value to 0.

    Set-ItemProperty -Path “HKLM:\SYSTEM\CurrentControlSet\Control\Lsa” -Name “disabledomaincreds” -Value 0

  3. Below PowerShell command will return the list of websites and binding information

    Import-Module WebAdministration

    Get-WebSite

  4. Force restart the computer

    Restart-Computer -Force

  5.  Invoke command on remote computer

    Invoke-Command -ComputerName Server01 -ScriptBlock { $PSVersionTable }

     

  6. Add user account in administrator group using PowerShell script

    $group = [ADSI](“WinNT://”+$env:COMPUTERNAME+”/administrators,group”)

    $group.add(“WinNT://$octopusServiceAccount,user”)

  7. Run installer in silent mode using PowerShell Script

    msiexec /i “installer.msi” /quiet

 

Posted in Microsoft Technology Tagged with:

Ads