At work I try to use powershell just to learn at least a few things about it. Of course I could use zsh, bash or any other unix shell, but that would be no fun, would it?
head & tail
What I was missing at the beginning, were unix head and tail, this one's quite easy.
head:
- (hg glog)[0..20]
tail:
- (hg glog)[-20..-1]
Why not reverse the orser of lines in head? No problem
- (cat somefile.txt)[20..0]
We can even use tail and head at once with (In case of hg glog this probably isn't the best idea ;)):
- (hg glog)[-20 .. 20]
for each...
I often have to run some command on files in given dir, in *nix systems I'd probably use something like:
- find dir -exec foobar {} \;
In powershell I do it like this:
- ls -r foo | % { .\doNoEvil.exe $_.get_FullPath(); }
- ls -r foo | % { .\doNoEvil.exe $_.FullName; }
% is an alias for ForEach-Object commandlet. $_ is a special pipeline variable, which holds reference to current object.
Hexdump
In unices I usually use xxd or od if xxd is not present, In powershell I use the following one-liner, I've split it to several lines to make it easier to read:
- (gc -Encoding byte -readCount 16 -totalCount 64 somefile.bin)[1..2] |
- %{
- $dmp = $_ |
- %{
- ("{0:x2}" -f $_)
- }
- "$dmp"
- }
gc is an alias for Get-Content commandlet. In the first line
I read 64 bytes of input in blocks of 16 bytes. Get-Content returns an array of 4 16-bytes block, I choose blocks 1-2 using subscript.
If you know where's the data you'd like to look at, it's
The codes in lines 2-8 is executed for each 16-bytes block, so $_ in third line
refers to 16byte block of data. That block is passed to inner loop in lines 4-6.
The loop executes strange-looking ("{0:x2}" -f $_) on every byte. This line
formats a byte using .net formatter you can read more about it on msdn Format.String page.
So formatted string is assigned to $dmp value which is print to the screen in line 7.
Now time for a better trick that I leave you for analysis:
- ls | % {
- $fn=$_;
- $sig = (gc -Encoding byte -readCount 4 -totalCount 32 $_)[7]
- if ($sig[0] -eq 0x53 -or $sig[0] -eq 0x47 -or $sig[0] -eq 0x4d) {
- .\doNoEvil.exe $fn;
- }
- }
Other usefull stuff
By default executing python script in powershell will spawn a new shell (which is baad thing, because we can't play with the output).
Solution is to add python scripts extenstion to PATHEXT environment
variable. From powershell console this can be made using following syntax:
- $env:pathext += ";.py"
Alternatively you can change is system-wide in control panel => System => Advanced System Settings => Advanced (card) => Environment Variables.
A bit annoying thing is that by default powershell converts output of command outomagically into internal representation, so running for example:
- python superscript.py > blurp.txt
Will generate output in utf-16 (that probably depends on your environment, dunno).
What you have to do is use Out-File commandlet:
- python superscript.py | out-file -encoding UTF8 blurp2.txt
Supposedly you could play with $OutputEncoding variable, I've tried, but it seems it does a bit different thing than I'd like it to do. If you'll find sollution, please gimme a tip :)
P.S. For the DDL guys, pleas be patient, I have lot of other things to do ATM
gim.org.pl is down






