I now consider myself to be intermediate level with PowerShell, so a lot of these tips I just ticked off as, yep cool, know that. I have decided to share the stuff I thought worth copying and share with you. If you think there are some good little snipits here, go to the site and check out the other (but still cool) stuff. #copy paste - clipboard function Get-Clip {[System.Windows.Forms.ClipBoard]::GetText()}
function Set-Clip {[System.Windows.Forms.ClipBoard]::SetText("sss")}
set-clip
get-clip
#try catch try {
dir nonexistent: -errorAction Stop
}
catch {
"Something strange occurred: $_"
}
#ping list function - check for host before running WMI query etc filter Check-Online {
trap { continue }
. {
$obj = New-Object system.Net.NetworkInformation.Ping
$result = $obj.Send($_, 1000)
if ($result.status -eq 'Success') { $_ }
}
}
"127.0.0.1","no.exists","google.com" | Check-Online
#get process owner(s) could be used to return all users logged into a server if you checked explorer.exe
$processes = Get-WmiObject Win32_Process -Filter "name='notepad.exe'"
$appendedprocesses = foreach ($process in $processes) {
Add-Member -MemberType NoteProperty -Name Owner -Value (
$process.GetOwner().User) -InputObject $process -PassThru }
$appendedprocesses | ft name, owner
#host 2 IP function Get-HostToIP($hostname) {
$result = [system.Net.Dns]::GetHostByName($hostname)
$result.AddressList | ForEach-Object {$_.IPAddressToString }
}
#get installed software (piped to gridview) Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* |
Select-Object DisplayName, DisplayVersion, Publisher, InstallDate, `
HelpLink, UninstallString | Out-GridView
#get REMOTE eventlog get-eventlog application -computername PC01234 -newest 1000 | where {$_.eventid -eq "20"}
#or
Get-EventLog System -EntryType Warning -ComputerName PC01234
#get a temporary file name ( Get-Date -format 'yyyy-MM-dd hh-mm-ss' ) + '.tmp'
#or
[io.path]::GetTempFileName()
#open explorer.exe at current directory ii .
#or
explorer .
#return domain connected to try { [ADSI]"" | Out-Host } catch { "Not connected to a domain" }
#Return dot net versions installed dir $env:windir\Microsoft.NET\Framework\v* -name
#return cleartext from get-credential $cred = Get-Credential
$pwd = [Runtime.InteropServices.Marshal]::PtrToStringAuto( [Runtime.InteropServices.Marshal]::SecureStringToBSTR( $cred.Password ))"
Password: $pwd"
#the format command is automated by passing "volume1" name and "Y"
#"Volume", "Y" | Format i: /FS:NTFS /Q
#dangerous command, it just demonstrates how to script the user typing Y and then the VolumeName for format.exe #download file in background using bits, saves in root of profile with same file name. Import-Module BitsTransfer
$url = '
http://powershell.com/cs/Themes/powershell/images/ps/title-logo.png' $fileName= $url.substring($url.lastindexofany("/")+1,$url.length - $url.lastindexofany("/")-1)
$target = $home+"\"+$fileName
Start-BitsTransfer -source $url -destination $target
#Pity I can't edit my posts here otherwise I would probably add to this in the future. :)
<message edited by TomRiddle on Thursday, July 29, 2010 3:28 PM>