Skip to content

cant properly convert this file to html if you can track down the problem #5

@Vibhu2

Description

@Vibhu2

My collection of Powershell Snippets

  1. To get last boot time of the machine.
   (get-date) - (Get-ComputerInfo).OsLastBootUpTime

or

      (Get-ComputerInfo).OsLastBootUpTime       

or

      (Get-CimInstance Win32_OperatingSystem).LastBootUpTime
  1. To get list of stopped services.
   Get-Service | Where-Object {$_.Status -eq "Stopped" -and $_.Starttype -eq "Automatic"}
  1. Setting execution Policy on machine.
   Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser
  1. Find Drive space.
   Get-PSDrive -PSProvider FileSystem
  1. Get Free Space for System Drive.
(Get-PSDrive $Env:SystemDrive.Trim(':')).Free/1GB
  1. Check BIOS information.
(Get-CimInstance Win32_BIOS)
  1. Check computer information
Get-CimInstance Win32_ComputerSystem 
  1. Check printer information on a machine.
 Get-CimInstance Win32_Printer | Select-Object Name, PortName, Default| Format-List
  1. Keep System Awake ( VB ) Script.
 set wsc = CreateObject("WScript.Shell")
Do
    'Five minutes
    WScript.Sleep(5*60*1000)
    wsc.SendKeys("{F13}")
Loop
  1. Install Powershell using chocolaty.
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

choco install powershell -force --yes
choco install powershell-core -force --yes
Start-Sleep -Seconds 90
exit
exit
  1. Installing winget on a machine using commandline.
invoke-webrequest
  1. Checking who rebooted a production server.
Get-EventLog –Log System –Newest 100 | Where-Object {$_.EventID –eq 1074} | FT MachineName, UserName, TimeGenerated -AutoSize
  1. check Computer domain
(Get-CimInstance Win32_ComputerSystem).Domain
  1. Restart LTServices.
Restart-Service -Name ltsvcmon
Restart-Service -Name labvnc
Restart-Service -Name ltservice
Stop-Process -Name ltsvcmon.exe -Force
Stop-Process -Name ltsvc.exe -Force
Stop-Process -Name lttray.exe -Force
Stop-Process -Name labvnc.exe -Force
Stop-Process -Name labtechupdate.exe -Force

Active Directory one liners

Before running any ActiveDirectory commands we need to import Ad-Module.

Import-Module ActiveDirectory

  1. Get All Members of a Group by name and ID
Get-ADGroupMember -Identity <group_name> -Recursive | select name,SamAccountName
  1. Finding inactive users (no activity for 195 days or more)
write-Host "Getting inactive User accounts the are enabled" n

$inacUser = Search-ADAccount -AccountInactive -TimeSpan $tspan -UsersOnly |Where-Object { $_.Enabled -eq $true } |select name,DistinguishedName,LastLogonDate

Write-host $inacuser.count -foreground Green Number of inactive user accounts that are enabled
  1. Finding New Users created in last 7 days.
write-Host "Getting users created within a week." n

$ADuserInWeek = Get-ADUser -Filter {whenCreated -ge $week} -Properties Whencreated | select Name,whenCreated,DistinguishedName

Write-Host $ADUserinweek.count -ForegroundColor Green Number of users created in the last 7 days `n
  1. Find Domain Controllers on Your Domain.
Resolve-DnsName -Type ALL -Name _ldap._tcp.dc._msdcs.$env:userdnsdomain
  1. List Software Available for Uninstall .
Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table
  1. Install PowerShell Core (6 and 7).
Invoke-Expression "& { $(Invoke-RestMethod -Uri aka.ms/install-powers) }" -UseMSI -Preview
  1. Get ADuser last logon information of all the users
Get-ADUser -Filter {enabled -eq $true} -Properties LastLogonTimeStamp | Select-Object Name,@{Name="Stamp"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp).ToString('dd-MM-yyyy_hh:mm:ss')}}
  1. Back up all production Group Policy Objects
Backup-GPO –All –Path C:\Temp\AllGPO

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions