What are the some of the PowerShell tips & tricks that you use to increase your productivity as a .NET developer?
Easily try out String.Format formats
Great article on String.Format formats by SteveX - String Formatting in C#
Using String.Format method
PS> [string]::Format("{0:C}", 1234567890) $1,234,567,890.00 PS> [string]::Format("{0:(###) ###-####}", 8005551212) (800) 555-1212
Using PowerShell "-f" format operator
PS> "{0:C}" -f 1234567890 $1,234,567,890.00 PS> "{0:(###) ###-####}" -f 8005551212 (800) 555-1212
Find out length of a text - copy & paste text into powershell and get length of string
- I usually use short cut to paste (ALT+SPACE+E+P) into PowerShell console.
PS> "Does this text fit into varchar(50) column?".length 43
Find out Assembly Qualified Name of a type (Assembly should be loaded into AppDomain for this to work for assemblies that are not in GAC)
Update: Simpler version (by Richard in the comment)
PS> ([System.String]).AssemblyQualifiedName
PS> [System.String] | select { $_.UnderlyingSystemType.AssemblyQualifiedName } $_.UnderlyingSystemType.AssemblyQualifiedName --------------------------------------------- System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Find an ASCII code for a character or vice versa
- Works on Unicode characters as well.
PS> [int][char]'a' 97 PS> [int][char]'A' 65 PS> [char][int]97 a PS> [char][int]65 A #"Sung" in Korean PS> [int][char]'승' 49849
I find PowerShell quite handy when I need to test if my regular expression matches the text.
$r = [regex]'date\s\d+-\d+-\d+\s\d+:\d+'
$r.Matches('date 2008-01-01 10:20 some text date 2008-01-02 11:13') | % { $_.value }
Keith Hill added some code that uses some conversion to base 64. So, it's what I have in my profile as well:
function FromBase64($str) {
[system.text.encoding]::utf8.getstring( [system.convert]::frombase64string($str))
}
function ToBase64($str) {
[system.convert]::tobase64string([system.text.encoding]::utf8.getBytes($str))
}
I use PowerShell to explore and test the functionality of DLL's I've not used before. Loading an assembly in PowerShell and using Get-Member to examine it is a quick way to dig into different types.
Add a "PowerShell at Solution" to VS's tools menu:
Tools | External Tools... and as follows:
Title: PowerShell at SolutionM
Command: %WinDir%\system32\windowspowershell\v1.0\powershell.exe
Arguments: -noexit -command "set-title ('PowerShell Solution ' + (Get-Item $(SolutionFileName)).BaseName)"
Initial Directory: $(SolutionDir)
Set-Title is a helper function in my profile (simplified version):
function Set-Title {
param([string]$title)
$Host.UI.RawUI.WindowTitle = $title
}
PowerShell as Calculator
PS>1 + 1
2
PS>23 * -3 / [Math]::Log10(256)
-28.6516298184035
# Search for references in VS solution files:
gci . -r *.??proj | select-string '<Reference Include="System\.Windows\.Forms'
Get-ExceptionForHR 0x80004004 # Requires PowerShell Community Extension (PSCX)
Get-ExceptionForWin32 10 # Requires PSCX
PS> '<a><b></a></b>' | Test-Xml # Require PSCX
False
PS> Format-Hex .\EchoArgs\EchoArgs.csproj -count 8 -Columns 8 # Requires PSCX
Address: 0 1 2 3 4 5 6 7 ASCII
-------- ----------------------- --------
00000000 EF BB BF 3C 3F 78 6D 6C ...<?xml
Get-Clipboard | Split-String -sep `n | Out-Clipboard -Width 999 # Requires PSCX
# Check if binary is .NET assembly or not
PS> Test-Assembly $pshome\powershell.exe # Requires PSCX
False
$b64 = ConvertTo-Base64 Foo.dll -NoLineBreak
# Experimenting with Xml and XPath
PS> $url = 'http://keithhill.spaces.live.com/feed.rss'
PS> $rss = [xml](new-object System.Net.WebClient).DownloadString($url)
PS> $rss.SelectNodes('//title')
#text
-----
PowerShell Function Names
Image File Resizing Using the PowerShell Community Extensions
Customizing PowerShell ISE with Yank Line CustomMenu Item
Effective PowerShell: The Free eBook
Effective PowerShell Item 14: Capturing All Output from a Script
FYI, PowerShell Community Extensions was written to be the MKS Toolkit/cygwin add-on for PowerShell. It's focus is slight more towards developers than it is admins.
Coming to Powershell from bash, I found this article for getting history via .profile useful. Persist Command History