<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US"><title type="html">Lior Elia - Dev Blog</title><subtitle type="html">Mostly Dev related stuff, C#, PowerShell and Regular expressions are my current tools of the trade so you'll be reading about them a lot.</subtitle><id>http://blogs.msdn.com/lior/atom.xml</id><link rel="alternate" type="text/html" href="http://blogs.msdn.com/lior/default.aspx" /><link rel="self" type="application/atom+xml" href="http://blogs.msdn.com/lior/atom.xml" /><generator uri="http://communityserver.org" version="2.1.61025.2">Community Server</generator><updated>2009-05-27T08:31:00Z</updated><entry><title>PowerShell indentation for VIM - version 2.0</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/lior/archive/2009/10/18/powershell-indentation-for-vim-version-2-0.aspx" /><id>http://blogs.msdn.com/lior/archive/2009/10/18/powershell-indentation-for-vim-version-2-0.aspx</id><published>2009-10-18T20:19:00Z</published><updated>2009-10-18T20:19:00Z</updated><content type="html">&lt;p&gt;In continuation to my &lt;a href="http://blogs.msdn.com/lior/archive/2009/10/04/powershell-indentation-file-for-vim.aspx" target="_blank" mce_href="http://blogs.msdn.com/lior/archive/2009/10/04/powershell-indentation-file-for-vim.aspx"&gt;previous post&lt;/a&gt;, I've been working lately on fixes to my PowerShell indentation and they are finally complete.&lt;/p&gt;&lt;p&gt;Right now it looks like everything is indented correctly and i've uploaded the new version (2.0) to vim.org site.&lt;br&gt;&lt;/p&gt;&lt;p&gt;I had 39 downloads for version 1.0 (which is pretty impressive for something as niche as writing the most modern script language using one of the oldest editors around). Hopefully version 2.0 will be just as popular.&lt;br&gt;&lt;/p&gt;&lt;p&gt;So, go ahead and download the new version, it has everything you ever wanted from a PowerShell indentation script.&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;a href="http://www.vim.org/scripts/script.php?script_id=2800" title="PowerShell indentation script for VIM" target="_blank" mce_href="http://www.vim.org/scripts/script.php?script_id=2800"&gt; Download Here&lt;/a&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;Oh, and please rate if you find it useful. &lt;br&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9908819" width="1" height="1"&gt;</content><author><name>liorelia</name><uri>http://blogs.msdn.com/members/liorelia.aspx</uri></author><category term="PowerShell" scheme="http://blogs.msdn.com/lior/archive/tags/PowerShell/default.aspx" /><category term="VIM" scheme="http://blogs.msdn.com/lior/archive/tags/VIM/default.aspx" /></entry><entry><title>PowerShell indentation file for VIM</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/lior/archive/2009/10/04/powershell-indentation-file-for-vim.aspx" /><id>http://blogs.msdn.com/lior/archive/2009/10/04/powershell-indentation-file-for-vim.aspx</id><published>2009-10-04T12:02:00Z</published><updated>2009-10-04T12:02:00Z</updated><content type="html">&lt;P&gt;I've been writing my PowerShell scripts in &lt;A title="What's vim?" href="http://www.vim.org/about.php" target=_blank mce_href="http://www.vim.org/about.php"&gt;VIM&lt;/A&gt; for a while now. &lt;BR&gt;I've downloaded the syntax, ftplugin and indent scripts from &lt;A href="http://www.vim.org/scripts/index.php" target=_blank mce_href="http://www.vim.org/scripts/index.php"&gt;vim site&lt;/A&gt; but I've found one annoying problem. &lt;BR&gt;The original ps1.vim indent script was mostly a stub that assumed PowerShell indentation is exactly the same as C/C++/C#. &lt;BR&gt;Well, mostly it is. There is one little (but &lt;EM&gt;annoying&lt;/EM&gt;) difference. &lt;BR&gt;In PowerShell - a line beginning with the '#' sign is a comment line and should be indented as any other code line in that indentation level. &lt;BR&gt;In the C language variants, however, lines beginning with '#' are precompiler lines which are never indented.&lt;/P&gt;
&lt;P&gt;And so,&amp;nbsp;as the old saying goes - &lt;EM&gt;"&lt;SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: HE"&gt;Necessity &lt;/SPAN&gt;is the mother of all inventions"&lt;/EM&gt; - i sat down this morning and started to write my very &lt;EM&gt;first &lt;/EM&gt;VIM script: PowerShell indentation.&lt;/P&gt;
&lt;P&gt;You can find it in the scripts repository &lt;A href="http://www.vim.org/scripts/script.php?script_id=2800" target=_blank mce_href="http://www.vim.org/scripts/script.php?script_id=2800"&gt;here&lt;/A&gt;. Free for all.&lt;BR&gt;I think that for a first-timer it's not too bad.&lt;BR&gt;Please let me know if you find any bugs or wish to add functionality (but I don't promise anything).&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9902896" width="1" height="1"&gt;</content><author><name>liorelia</name><uri>http://blogs.msdn.com/members/liorelia.aspx</uri></author><category term="PowerShell" scheme="http://blogs.msdn.com/lior/archive/tags/PowerShell/default.aspx" /><category term="VIM" scheme="http://blogs.msdn.com/lior/archive/tags/VIM/default.aspx" /></entry><entry><title>Finding lines NOT containing certain words - RegEx magic</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/lior/archive/2009/06/28/finding-lines-not-containing-certain-words-regex-magic.aspx" /><id>http://blogs.msdn.com/lior/archive/2009/06/28/finding-lines-not-containing-certain-words-regex-magic.aspx</id><published>2009-06-28T17:47:34Z</published><updated>2009-06-28T17:47:34Z</updated><content type="html">&lt;p&gt;Regular expressions are full of surprises. Just when you thought you’ve seen it all, out comes a “what if I want to do this, but also that and match these but not those” - and the worse thing is that it is usually &lt;em&gt;my&lt;/em&gt; idea in the first place!&lt;/p&gt;  &lt;p&gt;So here’s an interesting one: While working on the &lt;a href="http://blogs.msdn.com/fxcop/" target="_blank"&gt;FxCop&lt;/a&gt; reports for our product, I wanted to find (and eventually replace) all public members in the code.     &lt;br /&gt;My first thought was this:&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt;.*;$&lt;/pre&gt;

&lt;p&gt;But this expression brought back thousands of false-positives like delegates, events, read-only fields, constants, etc. which are perfectly legal, even under the strict rules of FxCop. I needed something that would filter out the lines that contain certain keywords.&lt;/p&gt;

&lt;p&gt;After rummaging the &lt;a href="http://msdn.microsoft.com/en-us/library/aa293063(VS.71).aspx" target="_blank"&gt;MSDN page for regular expressions&lt;/a&gt; and some trial and error I found that the following regular expression did the trick quite nicely, while keeping a good false-positive ratio:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; ~(&lt;span class="kwrd"&gt;const&lt;/span&gt;|&lt;span class="kwrd"&gt;event&lt;/span&gt;|&lt;span class="kwrd"&gt;delegate&lt;/span&gt;|&lt;span class="kwrd"&gt;readonly&lt;/span&gt;|&lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt;|&lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;extern&lt;/span&gt;|&lt;span class="kwrd"&gt;abstract&lt;/span&gt;).*;$&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;In English:&lt;/strong&gt; look for lines with the word &lt;em&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt;&lt;/em&gt;, not followed by any of the words &lt;font color="#0000ff"&gt;&lt;em&gt;const&lt;/em&gt;&lt;/font&gt;,&lt;em&gt; &lt;font color="#0000ff"&gt;event&lt;/font&gt;&lt;/em&gt;,&lt;em&gt; &lt;font color="#0000ff"&gt;delegate&lt;/font&gt;&lt;/em&gt;, etc. and that end with '&lt;font color="#0000ff"&gt;;&lt;/font&gt;'&lt;/p&gt;

&lt;p&gt;The rarely used&amp;#160; &lt;font face="Courier New" color="#0000ff" size="2"&gt;~()&lt;/font&gt; syntax allows for specifying words that will &lt;b&gt;break&lt;/b&gt; the match if found in the specified location.&lt;/p&gt;

&lt;p&gt;You can also modify the above RegEx to support &lt;em&gt;replacing&lt;/em&gt; the faulty line with &lt;a href="http://msdn.microsoft.com/en-us/library/bb384054.aspx" target="_blank"&gt;auto-implemented properties&lt;/a&gt; (new in .NET 3.0).&lt;/p&gt;

&lt;pre class="csharpcode"&gt;{&lt;span class="kwrd"&gt;public&lt;/span&gt; ~(&lt;span class="kwrd"&gt;const&lt;/span&gt;|&lt;span class="kwrd"&gt;event&lt;/span&gt;|&lt;span class="kwrd"&gt;delegate&lt;/span&gt;|&lt;span class="kwrd"&gt;readonly&lt;/span&gt;|&lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt;|&lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;extern&lt;/span&gt;|&lt;span class="kwrd"&gt;abstract&lt;/span&gt;).*};$ &lt;/pre&gt;

&lt;p&gt;And use the following string in the “Replace with” field to make an auto-implemented property:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;\1 { get; set; }&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;A word of warning:&lt;/strong&gt; &lt;em&gt;Do not &lt;/em&gt;run a solution-wide find-replace using this method, as you might break things without noticing. Hand-pick your changes!&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9807440" width="1" height="1"&gt;</content><author><name>liorelia</name><uri>http://blogs.msdn.com/members/liorelia.aspx</uri></author><category term="RegularExpressions" scheme="http://blogs.msdn.com/lior/archive/tags/RegularExpressions/default.aspx" /></entry><entry><title>What, no HKCR in PowerShell?</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/lior/archive/2009/06/19/what-no-hkcr-in-powershell.aspx" /><id>http://blogs.msdn.com/lior/archive/2009/06/19/what-no-hkcr-in-powershell.aspx</id><published>2009-06-19T00:38:16Z</published><updated>2009-06-19T00:38:16Z</updated><content type="html">&lt;p&gt;&lt;font size="1"&gt;&lt;font color="#ff0000"&gt;This article was originally published &lt;/font&gt;&lt;a href="http://fortheloveofcode.wordpress.com/2008/06/08/what-no-hkcr-in-powershell/" target="_blank"&gt;&lt;font color="#ff0000"&gt;here&lt;/font&gt;&lt;/a&gt;&lt;/font&gt;&lt;font size="1"&gt;&lt;/font&gt;&lt;font color="#ff0000"&gt;&lt;font size="1"&gt;.&lt;/font&gt;&amp;#160;&lt;/font&gt;&lt;/p&gt;  &lt;h3&gt;Introduction&lt;/h3&gt;  &lt;p&gt;If you’ve been working with PowerShell for registry access, you’ve probably noticed by now that something is missing.&lt;/p&gt;  &lt;p&gt;PowerShell has two registry “drives” defined:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;HKLM&lt;/strong&gt; for &lt;strong&gt;HKEY_LOCAL_MACHINE&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;HKCU&lt;/strong&gt; for &lt;strong&gt;HKEY_CURRENT_USER&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The other registry roots are not defined.&lt;/p&gt;  &lt;h3&gt;The story behind this post&lt;/h3&gt;  &lt;p&gt;A while ago, a colleague of mine was trying to find and remove some entries from HKEY_CLASSES_ROOT and was having a hard time using RegEdit.&lt;/p&gt;  &lt;p&gt;So, I offered my help, while promoting my latest obsession – PowerShell.&lt;/p&gt;  &lt;p&gt;Imagine my embarrassment when I found out a minute later that there is no HKCR drive defined in PowerShell.&lt;/p&gt;  &lt;h3&gt;The solution&lt;/h3&gt;  &lt;p&gt;Fear not, my friends, for the HKLM and HKCU are merely there for convenience.&amp;#160; &lt;/p&gt;  &lt;p&gt;You can define by yourself, with a simple command, any registry drive you want.&lt;/p&gt;  &lt;p&gt;So, before working on the HKCR drive, we simply define it like so:&lt;/p&gt;  &lt;p&gt;New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT&lt;/p&gt; &lt;b&gt;&lt;font face="Courier New" color="#5f9ea0"&gt;&lt;font face="Courier New" color="#5f9ea0"&gt;       &lt;p&gt;New-PSDrive&lt;/p&gt;     &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;/font&gt;&lt;i&gt;&lt;font face="Courier New" color="#5f9ea0"&gt;&lt;font face="Courier New" color="#5f9ea0"&gt;-Name&lt;/font&gt;&lt;/font&gt;&lt;/i&gt;&lt;font face="Courier New"&gt; &lt;/font&gt;&lt;font face="Courier New" color="#800000"&gt;&lt;font face="Courier New" color="#800000"&gt;HKCR&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt; &lt;/font&gt;&lt;i&gt;&lt;font face="Courier New" color="#5f9ea0"&gt;&lt;font face="Courier New" color="#5f9ea0"&gt;-PSProvider&lt;/font&gt;&lt;/font&gt;&lt;/i&gt;&lt;font face="Courier New"&gt; &lt;/font&gt;&lt;font face="Courier New" color="#800000"&gt;&lt;font face="Courier New" color="#800000"&gt;Registry&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt; &lt;/font&gt;&lt;i&gt;&lt;font face="Courier New" color="#5f9ea0"&gt;&lt;font face="Courier New" color="#5f9ea0"&gt;-Root&lt;/font&gt;&lt;/font&gt;&lt;/i&gt;&lt;font face="Courier New"&gt; &lt;/font&gt;&lt;font face="Courier New" color="#800000"&gt;&lt;font face="Courier New" color="#800000"&gt;HKEY_CLASSES_ROOT&lt;/font&gt;&lt;/font&gt;&lt;/b&gt;   &lt;p&gt;Now, you have full access to HKEY_CLASSES_ROOT via HKCR.&lt;/p&gt;  &lt;p&gt;&lt;em&gt;Have fun. Be responsible.&lt;/em&gt;&lt;/p&gt;  &lt;h3&gt;P.S.&lt;/h3&gt;  &lt;p&gt;While on this subject, Ever noticed how you can’t use ‘-filter’ with the ‘dir’ command on registry drives?&lt;/p&gt;  &lt;p&gt;Well, you can pipe the results through the question mark filter to do the same job, though I’m quite sure there’s some performance penalty. Still, better than the old RegEdit.exe, right?&lt;/p&gt;  &lt;p&gt;Here’s an example:&lt;/p&gt; &lt;b&gt;&lt;font face="Courier New" color="#5f9ea0"&gt;&lt;font face="Courier New" color="#5f9ea0"&gt;       &lt;p&gt;cd     &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;/font&gt;&lt;font face="Courier New" color="#800000"&gt;&lt;font face="Courier New" color="#800000"&gt;HKLM&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;:\&lt;/font&gt;&lt;font face="Courier New" color="#800000"&gt;&lt;font face="Courier New" color="#800000"&gt;SOFTWARE&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;\ &lt;/font&gt;&lt;/b&gt;&lt;b&gt;&lt;font face="Courier New" color="#5f9ea0"&gt;&lt;font face="Courier New" color="#5f9ea0"&gt;       &lt;/p&gt;&lt;p&gt;dir      &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;| &lt;/font&gt;&lt;b&gt;&lt;font face="Courier New" color="#5f9ea0"&gt;&lt;font face="Courier New" color="#5f9ea0"&gt;?&lt;/font&gt;&lt;/font&gt;&lt;/b&gt;&lt;font face="Courier New"&gt;{&lt;/font&gt;&lt;font face="Courier New" color="#000080"&gt;&lt;font face="Courier New" color="#000080"&gt;$_&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;.Name &lt;/font&gt;&lt;font face="Courier New" color="#ff0000"&gt;&lt;font face="Courier New" color="#ff0000"&gt;-like&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt; &lt;/font&gt;&lt;font face="Courier New" color="#800000"&gt;&lt;font face="Courier New" color="#800000"&gt;“*Int*“}&lt;/font&gt;&lt;/font&gt;&lt;/b&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9777584" width="1" height="1"&gt;</content><author><name>liorelia</name><uri>http://blogs.msdn.com/members/liorelia.aspx</uri></author><category term="PowerShell" scheme="http://blogs.msdn.com/lior/archive/tags/PowerShell/default.aspx" /><category term="Registry" scheme="http://blogs.msdn.com/lior/archive/tags/Registry/default.aspx" /></entry><entry><title>Microsoft bing to replace Google as favorite search engine</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/lior/archive/2009/06/15/microsoft-bing-to-replace-google-as-favorite-search-engine.aspx" /><id>http://blogs.msdn.com/lior/archive/2009/06/15/microsoft-bing-to-replace-google-as-favorite-search-engine.aspx</id><published>2009-06-15T19:00:24Z</published><updated>2009-06-15T19:00:24Z</updated><content type="html">&lt;p&gt;&lt;a title="Try bing now!" href="http://www.bing.com/" target="_blank"&gt;&lt;img title="I-Love-Bing" style="border-right: 0px; border-top: 0px; display: inline; margin-left: 0px; border-left: 0px; margin-right: 0px; border-bottom: 0px" height="240" alt="I-Love-Bing" src="http://blogs.msdn.com/blogfiles/lior/WindowsLiveWriter/e0dc7d8f588e_77BD/I-Love-Bing_3.jpg" width="320" align="right" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Well, at least on &lt;em&gt;my&lt;/em&gt; computers.    &lt;br /&gt;I’ve been using Google as my search engine for an eternity now. Frankly, I don’t even remember what came before that. It was &lt;em&gt;that &lt;/em&gt;long ago. Of course, being the Microsoft enthusiast that I am - I gave live.com a ping now and then, promising myself to try it for a few days and giving up rather shortly after. Yes, I admit, there was never a click between me and Microsoft’s search engine.&lt;/p&gt;  &lt;p&gt;Then came “bing”&lt;/p&gt;  &lt;p&gt;Given the supposition that bing is “live.com revamped” and that it is probably emerging from the very same codebase, I had little expectations from the new search engine. An improved live.com? possibly. A re-packaged, re-branded but otherwise unchanged live.com? probably. An exciting new search engine that will replace Google search in &lt;em&gt;my&lt;/em&gt; world? well, that thought didn’t even occur to me. That is, until I actually used bing.&lt;/p&gt;  &lt;p&gt;Bing is fast, smart, versatile, easy to use, and has tons of useful features. Among the most notable features I count: &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Smart video preview:&lt;/strong&gt; Hover your mouse cursor on the video thumbnail and you’ll get an instant preview of several scenes from that video, with sound, inside the thumbnail.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Image search to die for:&lt;/strong&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Several zoom levels for image thumbnails, with and without textual details. Changing zoom level is done instantly, no waiting for reload.&lt;/li&gt;    &lt;li&gt;Filter by size, layout, color, style (photo or illustration), and “people” which lets you filter images showing just faces or head &amp;amp; shoulders.&lt;/li&gt;    &lt;li&gt;Seamless, continuous results scrolling - just scroll down with your mouse wheel and more images are revealed. All the results are in a single page but since they load “on-demand” when scrolling to them - there’s no delay in the search.&lt;/li&gt;    &lt;li&gt;Hover over thumbnail to expose more details and actions - like getting similar results.&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;strong&gt;Suggestions and auto-corrections &lt;/strong&gt;- Yes, I know Google had that for a long time, now bing has that too.&lt;/p&gt;  &lt;p&gt;There are other interesting features, like the maps, news and shopping search categories, but since I’m not actually living in the states, it’s of little use to me.&lt;/p&gt;  &lt;p&gt;In addition to these features, I must point out that bing just looks &lt;em&gt;great&lt;/em&gt;. It is certainly more colorful and interesting than the competition’s Spartan search page. And yet, it loads (almost) instantly.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;So, in conclusion: There’s a new search engine in town, and it’s awesome. bing!&lt;/strong&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9753664" width="1" height="1"&gt;</content><author><name>liorelia</name><uri>http://blogs.msdn.com/members/liorelia.aspx</uri></author><category term="Google" scheme="http://blogs.msdn.com/lior/archive/tags/Google/default.aspx" /><category term="Microsoft" scheme="http://blogs.msdn.com/lior/archive/tags/Microsoft/default.aspx" /><category term="Bing" scheme="http://blogs.msdn.com/lior/archive/tags/Bing/default.aspx" /></entry><entry><title>Introducing new blog series - Bag of toys</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/lior/archive/2009/06/04/introducing-new-blog-series-bag-of-toys.aspx" /><id>http://blogs.msdn.com/lior/archive/2009/06/04/introducing-new-blog-series-bag-of-toys.aspx</id><published>2009-06-04T14:47:00Z</published><updated>2009-06-04T14:47:00Z</updated><content type="html">&lt;div align="center"&gt;&lt;img src="http://blogs.msdn.com/photos/lior/images/9707885/original.aspx" title="This logo indicates a post about PowerShell" alt="PS" mce_src="http://blogs.msdn.com/photos/lior/images/9707885/original.aspx" width="69" height="60"&gt;
&lt;img src="http://blogs.msdn.com/photos/lior/images/9707874/original.aspx" title="This logo indicates a post about C# or other Visual Studio related stuff" alt="C#" mce_src="http://blogs.msdn.com/photos/lior/images/9707874/original.aspx" width="69" height="60"&gt;&lt;/div&gt;
&lt;h3&gt;Introduction&lt;/h3&gt;
&lt;p&gt;I have many PowerShell scripts and functions lying around, some are preloaded in my profile, some I just use from time to time and can be found in PowerShell’s folder on one of my computers.&lt;/p&gt;
  
&lt;p&gt;Every time I am faced with a computer task (or a whim) that takes more then 2 minutes to accomplish by hand – I turn to PowerShell (or VIM console for text manipulations, but that’s another story).&lt;/p&gt;
  
&lt;p&gt;So, I decided to share these tiny-but-useful scripts and functions by launching a blog series named “Bag of toys”.&lt;/p&gt;
  &lt;h3&gt;Script #1 – Get your external IP&lt;/h3&gt;  
&lt;p&gt;This little one-liner tells you your external IP address, the one exposed to the world, not the one you got from your ISP (which is much easier to find out, using IpConfig util).&lt;/p&gt;
  
&lt;pre&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;(&lt;/span&gt;&lt;span style="font-weight: bold; color: rgb(95, 158, 160);"&gt;New-Object&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;net.webclient&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;).downloadstring(&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;http://checkip.dyndns.com&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;) &lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;-replace&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt; &lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;[^\d\.]&lt;/span&gt;&lt;span style="color: rgb(128, 0, 0);"&gt;"&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;While this is quite self-explanatory to most PowerShell users, let’s just do a quick run-through to make sure we covered all the bases. So, from left to right:&lt;/p&gt;

&lt;ol&gt;
  
&lt;li&gt;We create a new WebClient object. &lt;/li&gt;

  
&lt;li&gt;And call its DownloadString method to get a string response from a web server. &lt;/li&gt;

  
&lt;li&gt;DynDns service is kind enough to expose a rather straight-forward url for getting our IP. &lt;/li&gt;

  
&lt;li&gt;If you try to execute this URL in your browser, you’ll get a plain-text page with the text - “Current IP Address: &lt;i&gt;your-ip-address&lt;/i&gt;” &lt;/li&gt;

  
&lt;li&gt;We don’t want the text, just the IP itself, so we use a &lt;span style="color: rgb(255, 0, 0);"&gt;-&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;replace &lt;/span&gt;to remove any character that is neither a digit (\d) nor a dot. &lt;/li&gt;
&lt;/ol&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9699333" width="1" height="1"&gt;</content><author><name>liorelia</name><uri>http://blogs.msdn.com/members/liorelia.aspx</uri></author><category term="PowerShell" scheme="http://blogs.msdn.com/lior/archive/tags/PowerShell/default.aspx" /><category term="C#" scheme="http://blogs.msdn.com/lior/archive/tags/C_2300_/default.aspx" /><category term="BagOfToys" scheme="http://blogs.msdn.com/lior/archive/tags/BagOfToys/default.aspx" /><category term="Network" scheme="http://blogs.msdn.com/lior/archive/tags/Network/default.aspx" /></entry><entry><title>Creating a debugger visualizer for Visual Studio (C# tutorial)</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/lior/archive/2009/05/31/creating-a-debugger-visualizer-for-visual-studio-c-tutorial.aspx" /><id>http://blogs.msdn.com/lior/archive/2009/05/31/creating-a-debugger-visualizer-for-visual-studio-c-tutorial.aspx</id><published>2009-05-31T19:24:06Z</published><updated>2009-05-31T19:24:06Z</updated><content type="html">&lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;font color="#ff0000" size="1"&gt;This article was originally published &lt;/font&gt;&lt;a href="http://blogs.microsoft.co.il/blogs/liore/archive/2009/04/18/how-to-create-a-debugger-visualizer.aspx" target="_blank"&gt;&lt;font color="#ff0000" size="1"&gt;here&lt;/font&gt;&lt;/a&gt;&lt;font color="#ff0000" size="1"&gt;.&lt;/font&gt;&lt;/p&gt;  &lt;h3&gt;What is a “debugger visualizer”?&lt;/h3&gt;  &lt;p&gt;Here’s a direct quote from MSDN: &lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;A visualizer creates a dialog box or other interface to displays a variable or object in a meaningful way that is appropriate to its data type. For example, an HTML visualizer interprets an HTML string and displays the result as it would appear in a browser window, a bitmap visualizer interprets a bitmap structure and displays the graphic it represents, and so on. Some visualizers allow you to edit as well as view the data.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Considering the context – custom debugger tools – creating a custom debugger visualizer is surprisingly easy. I would expect this to involve native code, COM objects, cumbersome registry editing and a week at a forced labor camp, or any combination of the above. Turns out it’s a walk in the park.&lt;/p&gt;  &lt;h5&gt;&lt;/h5&gt;  &lt;h3&gt;Wanna get started?&lt;/h3&gt;  &lt;p&gt;For those of you eager to begin, here is a summary of the steps needed. I’ll explain everything in detail after the jump. &lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Create a class library project. &lt;/li&gt;    &lt;li&gt;Add reference to Microsoft.VisualStudio.DebuggerVisualizers (.NET reference) &lt;/li&gt;    &lt;li&gt;Add reference to System.Windows.Forms (.NET reference) &lt;/li&gt;    &lt;li&gt;Create a class that inherits from DialogDebuggerVisualizer &lt;/li&gt;    &lt;li&gt;Implement the &lt;em&gt;single&lt;/em&gt; method required (very easy implementation). &lt;/li&gt;    &lt;li&gt;Add the DebuggerVisualizer assembly attribute to indicate which type are you handling and what is the class handling that type. &lt;/li&gt;    &lt;li&gt;Build the DLL and copy it to &lt;em&gt;&amp;lt;My Documents&amp;gt;&lt;/em&gt;\Visual Studio 2005\Visualizers &lt;/li&gt;    &lt;li&gt;When you’re debugging – hover over a variable of the type you’re handling and click the magnifying glass icon. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;That’s all you need to do.&lt;/p&gt;  &lt;h3&gt;Details explained&lt;/h3&gt;  &lt;p&gt;Steps 1-4 in the above list are pretty straight forward, so let’s jump right into step 5.&lt;/p&gt;  &lt;p&gt;I’ve decided to create my visualizer for the type &lt;a href="http://msdn.microsoft.com/en-us/library/system.drawing.color.aspx" target="_blank"&gt;Color&lt;/a&gt;. The visualizer will display a small form filled with the color specified in the variable inspected by the debugger.&lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Diagnostics;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Drawing; &lt;span class="rem"&gt;// add reference to System.Drawing .net lib for this line to compile.&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Windows.Forms;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; Microsoft.VisualStudio.DebuggerVisualizers;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;[assembly : DebuggerVisualizer(&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;  &lt;span class="kwrd"&gt;typeof&lt;/span&gt; (ColorVisualizer),&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;  Target = &lt;span class="kwrd"&gt;typeof&lt;/span&gt; (Color),&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;  Description = &lt;span class="str"&gt;&amp;quot;Color Visualizer&amp;quot;&lt;/span&gt;)]&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; ColorVisualizer : DialogDebuggerVisualizer {&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;  &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Show(IDialogVisualizerService svc, IVisualizerObjectProvider provider) {&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;    Color c = (Color) provider.GetObject();&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;    &lt;span class="kwrd"&gt;using&lt;/span&gt; (Form form = &lt;span class="kwrd"&gt;new&lt;/span&gt; Form()) {&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;      form.Text = &lt;span class="str"&gt;&amp;quot;Color Visualizer - by Lior Elia&amp;quot;&lt;/span&gt;;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;      form.BackColor = c;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;      form.Size = &lt;span class="kwrd"&gt;new&lt;/span&gt; Size(400, 400);&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;      svc.ShowDialog(form);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;    }&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;  }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;}&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;As&lt;strong&gt; &lt;/strong&gt;you can see, the code is really straight forward. Get the object, create a form (can be a pre-prepared form if it's a complex visualizer) and display the data. That's it!&lt;/p&gt;

&lt;p&gt;Another interesting thing to notice is regarding steps 7-8. You don't need to restart visual studio in order to use your new visualizer. This means that you can make changes and fixes to your code and just drop the new DLL in the visualizers folder in order for Visual Studio to use it. It's &lt;em&gt;that &lt;/em&gt;easy!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important note:&lt;/strong&gt; you need a reference to System.Drawing for the Color class to be accessible in your code.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9670311" width="1" height="1"&gt;</content><author><name>liorelia</name><uri>http://blogs.msdn.com/members/liorelia.aspx</uri></author><category term="C#" scheme="http://blogs.msdn.com/lior/archive/tags/C_2300_/default.aspx" /><category term="VisualStudioAddin" scheme="http://blogs.msdn.com/lior/archive/tags/VisualStudioAddin/default.aspx" /><category term="DebuggingTools" scheme="http://blogs.msdn.com/lior/archive/tags/DebuggingTools/default.aspx" /></entry><entry><title>Converting icons from 32bit to 16bit</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/lior/archive/2009/05/31/converting-icons-from-32bit-to-16bit.aspx" /><id>http://blogs.msdn.com/lior/archive/2009/05/31/converting-icons-from-32bit-to-16bit.aspx</id><published>2009-05-31T10:56:18Z</published><updated>2009-05-31T10:56:18Z</updated><content type="html">&lt;h3&gt;The story behind this post&lt;/h3&gt;  &lt;p&gt;Last week I ran into an interesting bug at work. We were extracting icons on one windows machine and saving them on an another windows machine as part of one of our features. This was a mechanism developed a while ago and it worked fine so far. Recently though, we began to notice certain icons on the target machine had variations of pinkish overlay clearly visible. Upon closer inspection of the icons and comparison to the originals it became clear that semi-transparent areas of the icon did not retain their transparency property at all. Instead, the color used for transparency marking (traditionally cyan) was saved to the colors layer; creating a visible pink-like overlay in the relevant areas of the icon.&lt;/p&gt;  &lt;p&gt;A little more testing and we found the culprit, sort of. We know it has something to do with moving an icon from a 16bit color depth machine to a 32bit one. Normally, we would investigate further and try to pin-point the root of this problem. However, since we are planning to move both systems to 32 bit color depth soon (before the product is released) we just needed something that would work for now, and that would be easy to cancel out once we make the move to 32bit. In short, a band aid.&lt;/p&gt;  &lt;h3&gt;&lt;/h3&gt;  &lt;h3&gt;How we got to downgrading the color bit depth of an icon&lt;/h3&gt;  &lt;p&gt;Now, if you read the previous section, you’d be a bit surprised to find out we want to &lt;em&gt;downgrade&lt;/em&gt; the icon from 32bit to 16bit. Wasn’t it the other way around? you ask. And you’re right, we &lt;em&gt;are&lt;/em&gt; extracting the icon on the 16bit machine, and my instinct was converting 16bit to 32bit. However, on &lt;em&gt;our &lt;/em&gt;specific configuration (which is a bit non-standard) we need to do the exact opposite. We extract the icon from whatever resource file it is in (exe, dll, icon file, icon library) and convert it &lt;em&gt;to 16bit&lt;/em&gt;. When we send this converted icon to the 32bit machine – the pink overlay is &lt;em&gt;gone!&lt;/em&gt; As simple as that. &lt;/p&gt;  &lt;p&gt;I’d like to add some final disclaimer note here though: Like I mentioned before, the configuration on the source machine is rather not-standard and there might be some bit depth duality in that system where several processes or even sessions run a different color bit depth at the same time. This might have caused that pink overlay anomaly but we’re not sure, and as I said in the previous section, it is not worth pursuing at the moment. &lt;/p&gt;  &lt;h3&gt;Converting an icon using the Bitmap class&lt;/h3&gt;  &lt;p&gt;It’s really quite easy. The Bitmap class contains a clone method which allows you to control the result’s bit depth as well as other useful parameters. The following code is a simplification of what we did in production code, done for clarity.&lt;/p&gt;  &lt;div class="csharpcode"&gt;&amp;#160;&lt;/div&gt;  &lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Drawing;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Drawing.Imaging;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.IO;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;&lt;span class="kwrd"&gt;class&lt;/span&gt; Sample {&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ConvertIcon() {&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;        &lt;span class="rem"&gt;// Loading the original icon&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;        Icon icon = &lt;span class="kwrd"&gt;new&lt;/span&gt; Icon(&lt;span class="str"&gt;&amp;quot;originalIcon.ico&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;        &lt;span class="rem"&gt;// Converting it to bitmap, to allow image manipulation&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;        Bitmap bitmap = icon.ToBitmap();&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;        &lt;span class="rem"&gt;// This rectangle has no meaning for us, it just has to encompass the entire image&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;        Rectangle r = &lt;span class="kwrd"&gt;new&lt;/span&gt; Rectangle(0, 0, bitmap.Width, bitmap.Height);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;        &lt;span class="rem"&gt;// Using the clone method to convert the icon&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;        Bitmap bmp = bitmap.Clone(r, PixelFormat.Format16bppArgb1555);&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;        &lt;span class="rem"&gt;// And we claim back the icon (to retain the ico format) and save it.&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;        icon = Icon.FromHandle(bmp.GetHicon());&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;        FileStream stream = &lt;span class="kwrd"&gt;new&lt;/span&gt; FileStream(&lt;span class="str"&gt;&amp;quot;convertedIcon.ico&amp;quot;&lt;/span&gt;, FileMode.Create);&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;        icon.Save(stream);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt;        stream.Close();&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;    }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9667757" width="1" height="1"&gt;</content><author><name>liorelia</name><uri>http://blogs.msdn.com/members/liorelia.aspx</uri></author><category term="Win32" scheme="http://blogs.msdn.com/lior/archive/tags/Win32/default.aspx" /><category term="Shell" scheme="http://blogs.msdn.com/lior/archive/tags/Shell/default.aspx" /><category term="GUI" scheme="http://blogs.msdn.com/lior/archive/tags/GUI/default.aspx" /><category term="Presentation" scheme="http://blogs.msdn.com/lior/archive/tags/Presentation/default.aspx" /><category term="Graphics" scheme="http://blogs.msdn.com/lior/archive/tags/Graphics/default.aspx" /><category term="Native" scheme="http://blogs.msdn.com/lior/archive/tags/Native/default.aspx" /><category term="C#" scheme="http://blogs.msdn.com/lior/archive/tags/C_2300_/default.aspx" /></entry><entry><title>The unwritten rules of changing the console window's size</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/lior/archive/2009/05/27/ResizePowerShellConsoleWindow.aspx" /><id>http://blogs.msdn.com/lior/archive/2009/05/27/ResizePowerShellConsoleWindow.aspx</id><published>2009-05-27T13:17:00Z</published><updated>2009-05-27T13:17:00Z</updated><content type="html">&lt;p&gt;When changing the console window size in PowerShell you must also change the underlying buffer size. Doing so might require a bit more than just copy-paste-replace-window-keyword-with-buffer-keyword. There are apparently special &lt;i&gt;rules &lt;/i&gt;for doing this without failing.&lt;br&gt;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;h4&gt;These are the unwritten (&lt;i&gt;but carefully upheld&lt;/i&gt;) rules of resizing the console window via a PowerShell script:&lt;/h4&gt;&amp;nbsp;&amp;nbsp; 1. The buffer width can't be resized to be &lt;i&gt;narrower&amp;nbsp; &lt;/i&gt;than the window's current width.&lt;br&gt;&amp;nbsp;&amp;nbsp; 2. The window's width can't be resized to be &lt;i&gt;wider&amp;nbsp; &lt;/i&gt;than the buffer's current width. &lt;br&gt;&lt;br&gt;So, if you want to make the console window &lt;i&gt;narrower&lt;/i&gt;, you set the &lt;b&gt;window size &lt;/b&gt;first.&lt;br&gt;But if you want to make the console window &lt;i&gt;wider&lt;/i&gt;, you must set the &lt;b&gt;buffer size&lt;/b&gt; first.&lt;br&gt;
&lt;p&gt;&lt;br&gt;Try not to follow these rules - and you get a lovely exception + the console window isn't resized. &lt;br&gt;&lt;br&gt;Now we have only one problem - The starting width of the console window may vary on different machines/resolutions.&lt;br&gt;&lt;br&gt;The solution - check the width &lt;i&gt;before &lt;/i&gt;setting and act accordingly.&lt;br&gt;&lt;br&gt;Here's an example:&lt;br&gt;&lt;br&gt;
&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;
&lt;/p&gt;
&lt;div class="csharpcode"&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;$width = 127&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;$sizeWindow = new-object System.Management.Automation.Host.Size $width,50&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;$sizeBuffer = new-object System.Management.Automation.Host.Size $width,9999&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;&lt;span class="kwrd"&gt;if&lt;/span&gt; ($Host.UI.RawUI.WindowSize.width &lt;span class="preproc"&gt;-gt&lt;/span&gt; $width) {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    $Host.UI.RawUI.WindowSize = $sizeWindow&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    $Host.UI.RawUI.BufferSize = $sizeBuffer&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;}&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;&lt;span class="kwrd"&gt;else&lt;/span&gt; {&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;    $Host.UI.RawUI.BufferSize = $sizeBuffer&lt;/pre&gt;
&lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;    $Host.UI.RawUI.WindowSize = $sizeWindow&lt;/pre&gt;
&lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9644255" width="1" height="1"&gt;</content><author><name>liorelia</name><uri>http://blogs.msdn.com/members/liorelia.aspx</uri></author><category term="PowerShell" scheme="http://blogs.msdn.com/lior/archive/tags/PowerShell/default.aspx" /></entry><entry><title>Hello MS World (ver 1.1)</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/lior/archive/2009/05/27/HelloMsWorld.aspx" /><id>http://blogs.msdn.com/lior/archive/2009/05/27/HelloMsWorld.aspx</id><published>2009-05-27T10:31:00Z</published><updated>2009-05-27T10:31:00Z</updated><content type="html">
&lt;p&gt;Writing the first post in my blog as "Hello World" became a habit, which probably means I should stop moving blogs all the time.&lt;/p&gt;
&lt;p&gt;That said, I'm happy to open my brand new spanking MSDN blog.&lt;/p&gt;
&lt;p&gt;It's just a move from one Microsoft site to another (more global and more technical) but just the same, I had to do the inevitable, and post a "Hello Wolrd" item.&lt;/p&gt;
&lt;p&gt;As before, you are welcome to read the (updated) &lt;a href="http://blogs.msdn.com/lior/about.aspx" title="About this blog, me, and the universe." target="_blank" mce_href="http://blogs.msdn.com/lior/about.aspx"&gt;about&lt;/a&gt; page.&lt;/p&gt;
&lt;p&gt;You may also want to read past posts in my &lt;a href="http://blogs.microsoft.co.il/blogs/liore" title="My blog @ Microsoft.co.il" target="_blank" mce_href="http://blogs.microsoft.co.il/blogs/liore"&gt;previous blog&lt;/a&gt;. &lt;/p&gt;
&lt;p&gt;Have fun reading, and always remember: &lt;i&gt;We are here to learn, Knowledge is Power.&lt;/i&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="blue" face="lucida console"&gt;Lior&lt;/font&gt;&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9643890" width="1" height="1"&gt;</content><author><name>liorelia</name><uri>http://blogs.msdn.com/members/liorelia.aspx</uri></author></entry></feed>