Register | Login

Stacking Code

public interface IBlog { string Dump(Stream consciousness); }

PowerShell I Should Already Know

Tuesday, 8 March, 2011 @ 7:56 PM < Adam Boddington
Tags: PowerShell

I'm new to PowerShell and only just now realising the power inherent in that little blue box (yes I know, welcome to 2006). This morning I had a groan moment when I discovered how easy it is to launch files using their default application. For example, if I have two files, one called Notes.txt and another called Staff Meeting Notes.txt, I can open them by typing...

.\Notes.txt
& '.\Staff Meeting Notes.txt'

Ampersand is the call operator in PowerShell and tells it to execute what follows, i.e. execute Staff Meeting Notes.txt, don't treat it as a string. Of course typing all that out is a bit painful. I can do this instead...

staff<tab>

Hitting tab is like autocomplete. I only need to type just enough to distinguish the file from other files, or if not, hit tab a couple times to cycle through all the matching files until I get the right file. Once it shows me the right file...

& '.\Staff Meeting Notes.txt'

I can hit enter to open the file.

If I'm not sure of the file name and want to use wildcards, no problem...

*.txt<tab>

Hitting tab repeatedly will cycle through every file that matches the wildcard expression. I don't even need to type xt if .txt is the only file extension starting with t in the directory.

*.t<tab>

It's like there is an implicit star at the end of the string when using tab -- like there was for staff<tab>.

What if I wanted to open everything that matches a wildcard expression? That's easy too. You're probably familiar with this command already...

dir *.txt

That will list everything with the .txt file extension. Great, but what can be done with it? Lots with the pipe operator in PowerShell. For example, the result can be piped to a for each loop to execute a command on each file.

dir *.txt | foreach { & $_ }

The foreach command runs something for everything it is given. My something is & (call) followed by $_ (the variable for the current item in the loop). Running this will open every text file in the current directory.

Power to the... shell.

There are 0 comments.


Comments

Leave a Comment

Please register or login to leave a comment.


Older
Windows Installer Woes

Newer
SQL Server 2008 Express Sysadmin

Older
Windows Installer Woes

Newer
SQL Server 2008 Express Sysadmin

browse with Pivot


About


Projects

Building Neno


RSS
Recent Posts

Codility Nitrogenium Challenge
OS X Lock
HACT '13
Codility Challenges
Priority Queue


Tags

Architecture (13)
ASP.NET (2)
ASP.NET MVC (13)
Brisbane Flood (1)
Building Neno (38)
C# (4)
Challenges (3)
Collections (1)
Communicator (1)
Concurrency Control (2)
Configuration (1)
CSS (5)
DataAnnotations (2)
Database (1)
DotNetOpenAuth (2)
Entity Framework (1)
FluentNHibernate (2)
Inversion of Control (5)
JavaScript (1)
jQuery (4)
Kata (2)
Linq (7)
Markdown (4)
Mercurial (5)
NHibernate (20)
Ninject (2)
OpenID (3)
OS X (1)
Pivot (6)
PowerShell (8)
Prettify (2)
RSS (1)
Spring (3)
SQL Server (5)
T-SQL (2)
Validation (2)
Vim (1)
Visual Studio (2)
Windows Forms (3)
Windows Service (1)


Archives


Powered by Neno, ASP.NET MVC, NHibernate, and small furry mammals. Copyright 2010 - 2011 Adam Boddington.
Version 1.0 Alpha (d9e7e4b68c07), Build Date Sunday, 30 January, 2011 @ 11:37 AM