Skip to content

Instantly share code, notes, and snippets.

@jeremybeavon
Last active August 22, 2016 03:41
Show Gist options
  • Save jeremybeavon/efc499fc42c8cbd170e09c11f17eaaa0 to your computer and use it in GitHub Desktop.
Save jeremybeavon/efc499fc42c8cbd170e09c11f17eaaa0 to your computer and use it in GitHub Desktop.
Handy powershell commands
# Test if type is loaded
if (-not ([System.Management.Automation.PSTypeName]'FixedIssues').Type)
{
# ...
}
# Convert array to string
$array | Out-String
# Access internet in powershell through proxy server
[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials
# Get Disk Space
$removableDisk = 2
$localDisk = 3
$networkDrive = 4
$compactDisc = 5
$ramDisk = 6
Get-WmiObject win32_logicaldisk -Computername . | `
Where-Object {$_.FreeSpace -and $_.DeviceType -eq $localDisk} | `
Select-Object -Property `
@{Name="Drive";Expression={$_.DeviceID}}, `
@{Name="FreeSpace";Expression={[string][math]::truncate($_.freespace / 1GB) + " GB (" + [int]($_.FreeSpace / $_.Size * 100) + "%)"}}
# Get default printer
Get-WmiObject -Class Win32_Printer | Where-Object { $_.Default }
# Set default printer
(Get-WmiObject -Class Win32_Printer -Filter "Name='...'").SetDefaultPrinter()
# Get list of installed programs
Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | `
Select-Object DisplayName, DisplayVersion | `
Where-Object {$_.DisplayName -ne $null} | `
Sort-Object DisplayName
# Get the restart times for a machine
Get-EventLog System -ComputerName . | `
Where-Object {$_.EventID -eq '6005'} | `
Sort-Object -desc TimeGenerated | `
Select-Object -Property @{Name='Approximate Machine Start Time';Expression={$_.TimeGenerated}}
# Create performance counters
logman create counter DevOps_Test -f csv -si 20 -o C:\DevOps_Test\DevOps_Test.csv -a -c "\PhysicalDisk(_Total)\% Idle Time" "\PhysicalDisk(_Total)\Avg. Disk sec/read" "\PhysicalDisk(_Total)\Avg. Disk sec/write" "\PhysicalDisk(_Total)\Current Disk Queue Length" "\Memory\Available MBytes" "\Memory\Pages/sec" "\Memory\Cache Bytes" "\Network Interface(*)\Bytes Total/sec" "\Network Interface(*)\Output Queue Length" "\Paging File\% Usage"
# DSC:
# This is useful if Get-PSRepository returns: WARNING: Unable to find module repositories.
Register-PSRepository -Name "PSGallery" –SourceLocation "https://www.powershellgallery.com/api/v2/" -InstallationPolicy Trusted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment