Some interesting PowerShell snipits.

Author Message
TomRiddle

  • Total Posts : 620
  • Scores: 12
  • Reward points : 0
  • Joined: 2/7/2008
  • Location: Australia
  • Status: offline
Some interesting PowerShell snipits. Thursday, July 29, 2010 3:24 PM (permalink)
0

I was trolling through http://powershell.com/cs/blogs/tips/ and this stuff is gold.

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>
-join([int[]][char[]]'Ut|jwXmjqq%Wzqjx'|%{[char]($_-5)})
 
#1
    ebgreen

    • Total Posts : 8227
    • Scores: 98
    • Reward points : 0
    • Joined: 7/12/2005
    • Status: offline
    Re:Some interesting PowerShell snipits. Tuesday, August 03, 2010 1:12 AM (permalink)
    0
    My favorite:

    ii .
    "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
    Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
    http://www.visualbasicscript.com/m_47117/tm.htm
     
    #2
      grace01

      • Total Posts : 2
      • Scores: 0
      • Reward points : 0
      • Joined: 8/10/2010
      • Status: offline
      Re:Some interesting PowerShell snipits. Tuesday, August 10, 2010 4:46 PM (permalink)
      0
      Hello Friends

      does anyone tell me window power shell
      i am not able to understand how it is helpful and how it is working
      if you know please tell me
      thnanks

      Have Nice Day

      ++++++++++++++++++++++++++++++++++++++++++++++++++++

      "Want to get-on Google's first page and loads of traffic to your website?

      Hire a SEO Specialist from Ocean Groups SEO SPECIALIST "

       
      #3

        Online Bookmarks Sharing: Share/Bookmark

        Jump to:

        Current active users

        There are 0 members and 1 guests.

        Icon Legend and Permission

        • New Messages
        • No New Messages
        • Hot Topic w/ New Messages
        • Hot Topic w/o New Messages
        • Locked w/ New Messages
        • Locked w/o New Messages
        • Read Message
        • Post New Thread
        • Reply to message
        • Post New Poll
        • Submit Vote
        • Post reward post
        • Delete my own posts
        • Delete my own threads
        • Rate post

        2000-2012 ASPPlayground.NET Forum Version 3.9