views:

183

answers:

3

I'm getting into PowerShell scripting and am impressed by its muscle, flexibility and grit, especially compared to Microsoft`s classic DOS command line implementations. Basically I want the community to gain quick wins from your experience with it.

What is your best PowerShell hyper-productivity script?
Rules of engagement for the script:

  • only one script per answer - your best,
  • preferably short and sweet,
  • general enough to apply to the masses otherwise it probably won't get many votes for applicability,
  • also provide any context needed to use your script. Thx.

The decision for emitting this question is based on this question and this question because neither specifically targets PowerShell.

I look forward to using the scripts you provide and know others will too, especially the fact the best ones will be voted to the top for easy picking - a great use of community evaluation.

Disclaimer: I realize the merits of questions like this are often highly debated, but will post and let the community decide. Thanks.

+2  A: 

Reduced typing, less errors and decreased noise.

Function ql {$args}

ql tom john harry | % {$_}

Without the ql function you need to add quotes " and commas ,

"tom", "john", "harry" | % {$_}

List of months

$months = ql Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
Doug Finke
`ql` = quote list? Why not `qw` (quote words) like in Perl?
Gabe
That works nicely.
John K
Good question. I thought it was quick list.It also works with creating arrays of numbers and functions. Not just words.
Doug Finke
+2  A: 

Recursively return all files from the current directory, but not the directories:

gci . *.* -rec | where { ! $_.PSIsContainer }
Andy
Very handy indeed. Also you just taught me about PS brevity: `gci` vs `Get-ChildItem`
John K
You also don't need the `*.*`, `gci . -r` will suffice.
Keith Hill
@Keith: Cool. You could even substitute 'where' for '?' to make it even more concise.
Andy
+2  A: 

Taking into account the rules of engagement it was not easy to choose a favorite from my arsenal… Well, this is not my best hyper-productivity script, indeed, but it fits all the rules and it really saves seconds here and there on exporting data to CSV that I do interactively quite often.

This is a trivial wrapper of the Export-Csv that adds .CSV extension to the specified output path and tells not to write type information (which is written by default and makes .CSV files not readable by all applications except PowerShell).

<#
.SYNOPSIS
    Export-Csv + auto CSV extension + NoTypeInformation

.DESCRIPTION
    Parameters are standard Export-Csv parameters but:
    -Path: extension .CSV is added if not yet
    -NoTypeInformation: the switch is added if not yet
#>

param
(
    $Path
)

if ($Path -and $Path -notlike '*.csv') {
    $PSBoundParameters['Path'] = $Path + '.csv'
}

if (!$PSBoundParameters.ContainsKey('NoTypeInformation')) {
    $PSBoundParameters.Add('NoTypeInformation', $true)
}

$input | Export-Csv @PSBoundParameters
Roman Kuzmin
Nifty wrapper .
John K