-
Notifications
You must be signed in to change notification settings - Fork 8
Open
Description
My collection of Powershell Snippets
- To get last boot time of the machine.
(get-date) - (Get-ComputerInfo).OsLastBootUpTimeor
(Get-ComputerInfo).OsLastBootUpTime or
(Get-CimInstance Win32_OperatingSystem).LastBootUpTime- To get list of stopped services.
Get-Service | Where-Object {$_.Status -eq "Stopped" -and $_.Starttype -eq "Automatic"}- Setting execution Policy on machine.
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser- Find Drive space.
Get-PSDrive -PSProvider FileSystem- Get Free Space for System Drive.
(Get-PSDrive $Env:SystemDrive.Trim(':')).Free/1GB- Check BIOS information.
(Get-CimInstance Win32_BIOS)- Check computer information
Get-CimInstance Win32_ComputerSystem - Check printer information on a machine.
Get-CimInstance Win32_Printer | Select-Object Name, PortName, Default| Format-List- Keep System Awake ( VB ) Script.
set wsc = CreateObject("WScript.Shell")
Do
'Five minutes
WScript.Sleep(5*60*1000)
wsc.SendKeys("{F13}")
Loop- 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- Installing winget on a machine using commandline.
invoke-webrequest- Checking who rebooted a production server.
Get-EventLog –Log System –Newest 100 | Where-Object {$_.EventID –eq ‘1074’} | FT MachineName, UserName, TimeGenerated -AutoSize- check Computer domain
(Get-CimInstance Win32_ComputerSystem).Domain- 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 -ForceActive Directory one liners
Before running any ActiveDirectory commands we need to import Ad-Module.
Import-Module ActiveDirectory
- Get All Members of a Group by name and ID
Get-ADGroupMember -Identity <group_name> -Recursive | select name,SamAccountName- 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”- 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- Find Domain Controllers on Your Domain.
Resolve-DnsName -Type ALL -Name _ldap._tcp.dc._msdcs.$env:userdnsdomain- List Software Available for Uninstall .
Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table- Install PowerShell Core (6 and 7).
Invoke-Expression "& { $(Invoke-RestMethod -Uri aka.ms/install-powers…) }" -UseMSI -Preview- 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')}}- Back up all production Group Policy Objects
Backup-GPO –All –Path C:\Temp\AllGPOReactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels