Wednesday, October 12, 2016

Misc PowerShell One-liners used in Day to Day operations

Here are some PowerShell commands we can to use to perform our day to day operations as Windows System Administrators:

  • Disable Server Manager from running at logon** :
Invoke-Expression 'REG ADD HKLM\SOFTWARE\MICROSOFT\SERVERMANAGER\ /T REG_DWORD /V DoNotOpenServerManagerAtLogon /D 1 /f' 
  • Enable Server Manager from running at logon**:
Invoke-Expression 'REG ADD HKLM\SOFTWARE\MICROSOFT\SERVERMANAGER\ /T REG_DWORD /V DoNotOpenServerManagerAtLogon /D 0 /f' 
  • Read text from file and perform any operation which is inside the {}:
Get-Content C:\Temp\servers.txt |foreach {Write-Host $_ -ForegroundColor RED} 
  • Invoke a command remotely on any remote server {net user is the command below} :
Invoke-Command -ComputerName Server1 {net user}

  • List out users who have ever logged on to a remote server ** :

Get-WmiObject -ComputerName Server1 -ClassName Win32_NetworkLoginProfile| select Name, FullName, LastLogon
  •  List out users who have ever logged on to a server **:

Get-WmiObject -ClassName Win32_NetworkLoginProfile| select Name, FullName, LastLogon

  •  Loop something to run until "n" number of times after every "nn" seconds :

1..9|foreach {Get-Date -format G ; Get-Hotfix |Group Installedon; Start-Sleep 60; Write-Host " `n"}




**The command needs to be run from an elevated PowerShell console (Start PowerShell as Admin).
 --------Stay tuned for more commands: