function Prompt { $host.ui.RawUI.WindowTitle = $(get-location) "PS" + ("." * ($NestedPromptLevel)) + "> " } function dirs {dir | where {$_.PSIsContainer}} function files {dir | where {-not $_.PSIsContainer}} function Set-ErrorColor { param([string]$ForegroundColor = "Red", [string]$BackgroundColor="Black") $a = (Get-Host).PrivateData $a.ErrorForegroundColor = $ForegroundColor $a.ErrorBackgroundColor = $BackgroundColor } # See also http://en.wikipedia.org/wiki/A440_(Concert_A) function stemvork {[Console]::Beep(440, 2000)} # A4 - 440 Herz 2 seconds # Get current CPU speed and temperature function cpu { get-wmiobject Win32_Processor | select CurrentClockSpeed,MaxClockSpeed | ft -AutoSize gwmi MSAcpi_ThermalZoneTemperature -Namespace "root\wmi" | select @{n="CurrentTemperature";e={($_.CurrentTemperature - 2732) / 10.0}}, @{n="CriticalTripPoint"; e={($_.CriticalTripPoint - 2732) / 10.0}} } # $quote = New-WebServiceProxy "http://swanandmokashi.com/HomePage/WebServices/QuoteOfTheDay.asmx?wsdl" # $quote.GetQuote() # Display the default display property set of an object # E.g.: ddps (gsv w3svc) or ddps ((gps)[0]) function ddps ([object]$obj) {$obj.psstandardmembers.DefaultDisplayPropertySet.ReferencedPropertyNames} # Execute a script using specified Culture # This function might be required to temporarily switch to en-en culture, e.g. when # automating Excel # Function Using-Culture ( [System.Globalization.CultureInfo]$culture = $(throw "USAGE: Using-Culture -Culture culture -Script {scriptblock}"), [ScriptBlock]$script= $(throw "USAGE: Using-Culture -Culture culture -Script {scriptblock}")) { $OldCulture = [System.Threading.Thread]::CurrentThread.CurrentCulture trap { [System.Threading.Thread]::CurrentThread.CurrentCulture = $OldCulture } [System.Threading.Thread]::CurrentThread.CurrentCulture = $culture Invoke-Command $script [System.Threading.Thread]::CurrentThread.CurrentCulture = $OldCulture } # create the Text-to-Speech object $voice = new-object -com sapi.spvoice filter out-voice () { $voice.speak($_) | out-null } # This function turns a string into a scriptblock # Usage: # $a = new-scriptblock "'Hello world! ' * 2" # &$a # # Note that PowerShell 2.0 has a slightly easier way, as the ScriptBlock type # has a static method called Create. # $sb = [ScriptBlock]::Create("Hello World"; Get-Date) # function New-ScriptBlock([string]$textofscriptblock) { $executioncontext.InvokeCommand.NewScriptBlock($textofscriptblock) } [reflection.assembly]::loadwithpartialname("microsoft.visualbasic") | out-null function inputbox ( [string]$msg = "Enter text please...", [type]$type= [string]) { [microsoft.visualbasic.interaction]::Inputbox($msg) | %{$_ -as $type} } # Update format data Update-FormatData $SampleScripts\myformats.format.ps1xml -erroraction silentlycontinue # Update Type info Update-TypeData $SampleScripts\MyOwnTypeExtensions.ps1xml -erroraction silentlycontinue function ghi {Get-History | Format-Table -View elapsed -AutoSize} New-Alias gel get-eventlog -force New-Alias ie "C:\program files\Internet Explorer\iexplore.exe" -force # Get the unique type names of the objects in the pipe function Get-Type { $input | %{$_.gettype()} | select -unique } new-alias gtp Get-Type -force # Get the unique type names of the objects in the pipe function Get-TypeTree { $input | %{$_.PSObject.typenames} } new-alias gtt Get-TypeTree -force # Get current thread id. By default, PowerShell starts each new pipe or scriptblock in a new thread function Get-ThreadId { [Threading.Thread]::CurrentThread.ManagedThreadId } set-alias gt Get-ThreadId # invoking the C# compiler new-alias csc "C:\Windows\Microsoft.NET\Framework64\v2.0.50727\csc.exe" -force # Load an assembly function Load-Assembly ([string]$name ) { [Reflection.Assembly]::LoadWithPartialName($name) } new-alias la Load-Assembly -erroraction silentlycontinue function Load-AssemblyFile ([string]$name ) { [Reflection.Assembly]::LoadFile($name) } new-alias laf Load-AssemblyFile -erroraction silentlycontinue # Load-Assembly("Microsoft.SharePoint") $MaximumHistoryCount = 1KB if (!(Test-Path ~\PowerShell -PathType Container)) { New-Item ~\PowerShell -ItemType Directory } function bye { Get-History -Count 1KB |Export-CSV ~\PowerShell\history.csv -UseCulture exit } if (Test-path ~\PowerShell\History.csv) { Import-CSV ~\PowerShell\History.csv -UseCulture |Add-History } function Get-Assemblies { [AppDomain]::CurrentDomain.GetAssemblies() } function Get-Types ($Pattern=".") { Get-Assemblies | %{ $_.GetExportedTypes() } | where {$_ -match $Pattern} } # For PSCredential objects (see Get-Credential) there's also the GetNetworkCredential() method function Get-SecureString ($SecurePassword) { $Ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($SecurePassword) $pwd = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($Ptr) [System.Runtime.InteropServices.Marshal]::ZeroFreeCoTaskMemUnicode($Ptr) $pwd } function spawn {"Level $($NestedPromptLevel+1)"; $host.EnterNestedPrompt(); "Level $($NestedPromptLevel)" } "`nNested prompt level: $NestedPromptLevel" # Nice example of using an XML Web Service over the Internet :-) # Function Get-Weather ([switch]$list, [string]$city = 'Eindhoven',[string]$country = 'Netherlands',[string]$filter = '') { if ($global:weather -eq $null) { $global:weather = New-WebServiceProxy -uri http://www.webservicex.com/globalweather.asmx?WSDL } if ($list) { ([xml]$global:weather.GetCitiesByCountry($filter)).NewDataSet.table } else { ([xml]$global:weather.GetWeather($City,$country)).CurrentWeather } } Function Alicante {Get-Weather -country spain -city 'Alicante / El Altet'} # trapping with ErrorAction is much faster than piping and filtering! #if (@(get-pssnapin powergadgets -ErrorAction SilentlyContinue ).count -eq 0) { # add-pssnapin powergadgets } Set-Alias installutil ` (Join-Path ` (Split-Path ([object].Assembly.Location) -Parent) ` installutil.exe) Set-Alias np notepad function signIt { Set-AuthenticodeSignature $args[0] @(Get-ChildItem cert:\CurrentUser\My -codesigning)[0] } # Cancelling a pipeline # http://powershell.com/cs/blogs/tobias/archive/2010/01/01/cancelling-a-pipeline.aspx # filter Stop-Pipeline([scriptblock]$condition={$true}) { $_ if (& $condition) { Throw (New-Object System.Management.Automation.PipelineStoppedException) } } . 'PS:\Sample scripts\setwindowpos.ps1'