views:

3304

answers:

12

What are the some of the PowerShell tips & tricks that you use to increase your productivity as a .NET developer?

+5  A: 
Sung Meister
+12  A: 

Easily try out String.Format formats

Great article on String.Format formats by SteveX - String Formatting in C#

  1. Using String.Format method

    PS> [string]::Format("{0:C}", 1234567890)
    $1,234,567,890.00
    PS> [string]::Format("{0:(###) ###-####}", 8005551212)
    (800) 555-1212
    
  2. Using PowerShell "-f" format operator

    PS> "{0:C}" -f 1234567890 
    $1,234,567,890.00 
    PS> "{0:(###) ###-####}" -f 8005551212 
    (800) 555-1212
    
Sung Meister
Or use the -f format operator (i.e. "{0:C}" -f 1234567890).
Emperor XLII
@Emperor XLII: Thanks, I have updated answer according to your suggestion.
Sung Meister
+4  A: 

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
Sung Meister
+4  A: 

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
Sung Meister
Simpler version of this: ([System.String]).AssemblyQualifiedName
Richard
Ah, awesome! Updated.
Sung Meister
+7  A: 

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
Sung Meister
Yes, I have used this and it is very handy.
JasonMArcher
+1 for being cool. I always wanted something like this.
Abbas
+3  A: 

I just found out a couple of days ago that PowerGUI comes with a source-level debugger. I had been ignoring PowerGUI because it appears to be mostly a sysadmin tool that isn't very useful to me, but the debugger that comes with it is awesome.

dangph
+11  A: 

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))
}
stej
Oh yeah I almost forgot. I usually use "-match" for quick and dirty regex check.
Sung Meister
PowerShell as Regex workbench, definitely.
Peter Seale
For regexes I really love switch -regex as well
Joey
+8  A: 

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.

Steven Murawski
I tend to overuse "[System.Reflection.Assembly]::LoadWithPartialName" from powershell even though it is marked as obsolete ;)
Sung Meister
It is pretty convenient..
Steven Murawski
+10  A: 

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
}
Richard
+1 This is awesome. instead of setting title, in my case, I change directory to $(SolutionDir); For some reason setting Initial Directory to $(SolutionDir) didn't not work for me and I have my title set to current directory, which is the behavior i didn't want to change. Thank you Richard.
Sung Meister
so my Argument is: -noexit -command "cd $(SolutionDir)"
Sung Meister
@Sung: You would need that if setting the title is triggered by your "cd", but without an initial set in your profile.
Richard
@Richard: Yes, my window title update is triggered by "cd". I can't stand typing "pwd" to find out where I am due to my short-term memory.
Sung Meister
+5  A: 

PowerShell as Calculator

PS>1 + 1
2
PS>23 * -3 / [Math]::Log10(256)
-28.6516298184035
Sung Meister
+8  A: 
# 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.

Keith Hill
+1 I can make use of those.
Sung Meister
A: 

Coming to Powershell from bash, I found this article for getting history via .profile useful. Persist Command History

nineowls