Updates from June, 2011 Toggle Comment Threads | Keyboard Shortcuts

  • tech0x20 8:25 pm on June 28, 2011 Permalink | Reply
    Tags:   

    Searching for a String in Environmental Variables in Powershell 

    I’ve updated the post through feedback in the comments:
    Regex compare on strings for Get-Variable is Where-Object -Match.

    So, the closest parallel to the following bash code:

    set | grep "Users"

    is the following Powershell:

    Get-Variable | Where-Object { $_.Value -Match "Users" }

    or shortened:

    gv | ? {$_.Value -Match "Users"}

    Original Post, with Updates
    I’m kind of dumbfounded how this:

    Get-Variable | Select-String -Pattern "Users"

    returns nothing.

    Yet,

    Get-Variable > gv.txt
    Get-Content gv.txt | Select-String -Pattern "Users"

    returns:

    $                              Users
    HOME                           C:UsersAdministrator
    PWD                            C:UsersAdministrator

    My initial thought was that maybe redirect captures standard output and standard error and the pipe doesn’t, but then, standard error should all come to the screen unfiltered if not captured by the pipe.

    Added:
    To add insult to injury, apparently

    Get-Variable -Include "PS*"

    or just

    Get-Variable "PS*"

    will do a wildcard search for variable names.

    Updated, per the comment from @jburger

    Get-Variable | Where-Object { $_.Value -ne $null } | Where-Object {$_.ToString().Contains("Users") }

    or

    gv | ? { $_.Value -ne $null } | ? {$_.Value.ToString().Contains(“Users”) }

    Playing with the Where-Object syntax, I was able to reduce this to:

    Get-Variable | Where-Object { $_.Value -ne $null -and $_.ToString().Contains("Users") }

    or

    gv | ? { $_.Value -ne $null  -and $_.Value.ToString().Contains(“Users”) }

    Definitely, this does the trick. However, I’m disappointed that PowerShell doesn’t automatically cast the object output using a ToString() operation.

     
    • Jim Burger 2:22 am on June 29, 2011 Permalink | Reply

      The thing here is that you have to remember that powershell is more of a programming language than a scripting lanugage. I.E Get-* will output typed objects, not string values. The upshot is that we can actually do some really complicated filtering… there is more to these objects than just their string data that you see output on the screen. 
      For instance… type:

      Get-Member -InputObject gv[0] 

      ..this will show all the members on the object type that gv outputs

      Note that the Value property is System.Object not System.String … this is why select-string doesn’t work.

      So…the ‘downside’ to powershell is you need to think like a .NET programmer.

      To answer you’re q, to do what you want without hitting the filesystem, you need to drill into the string representation of the Value property. I did it using powershells where clause. (there are probably better ways – Im no PS guru). In long form.

      Get-Variable | Where-Object { $_.Value -ne $null } | Where-Object { $_.Value.ToString().Contains(“Users”) }

      Short form

      gv | ? { $_.Value -ne $null } | ? {$_.Value.ToString().Contains(“Users”) }

      Now I consider myself a PS newbie – so there is probably a less verbose, neater way to do this. But htis is it from first principles AFAIK.

      Hope that Helps

      Jim Burger

    • Bartek Bielawski 4:19 am on June 29, 2011 Permalink | Reply

      To avoid disappointmens in future:
       
      1. Select-String is not grep. 😉 means: do not filter objects using it as you would filter command results in *nix shells.
      2. Contains is for array operations, not for searching the string.
      1,2,3,4 -contains 3
      3. Regex compare for strings is done with -match operator.
      That reduces all hard work to:
      gv | ? { $_.Value -match ‘Users’ }
      4. Simple pattern (‘*’, ‘?’ and ‘[]’) compare is done with -like operator.
      gv | ? { $_.Value -like ‘*[u-v]se?s*’ }
       
      BTW: get-help where-object -examples would probably get you there before I wrote my comment. Help i PS is really greate resources on how to use it… 🙂 To see all articles about operators you can do get-help operators. about_operators contains basic info that could also save you few dissapointments. 🙂
      Good luck!

  • tech0x20 6:53 am on July 8, 2010 Permalink | Reply
    Tags: about, help   

    Finding the about screen in Word 2007 

    I had to dig to find this:

    I used to be able to get the version number via the Help->About… functionality. Pretty standard Windows pattern.

    I wanted to see if the pushed update of Office 2007 SP2 had been applied this morning, and eventually had to look it up.

    It is now under the Office logo menu, which I expected. What I didn’t expect is that it’s buried under the Word Options selection at the bottom of that menu.

    In the Word Options menu, go to the Resources tab/menu, and the last item in the right pane of that window is the standard “About” information.

     
  • tech0x20 1:25 pm on December 15, 2008 Permalink | Reply  

    Every time you make an Excel spreadsheet this big, a kitten meets its maker 

    I’m not really that elitist about non-programmers using applications in an advanced way the programmers could find a better way to do. I’ve never had to port functionality from an Excel spreadsheet that was 2 MB in size prior to data being added–at least not more than once.

    However, I am a bit disturbed that Excel is the new Access:

    The total number of available columns in Excel
    Old Limit: 256 (2^8)
    New Limit: 16k (2^14)

    The total number of available rows in Excel
    Old Limit: 64k (2^16)
    New Limit: 1M (2^20)

    Number of unique colours allowed a single workbook
    Old Limit: 56 (indexed colour)
    New Limit: 4.3 billion (32-bit colour)

    Therefore, Excel is now Microsoft’s new small business data warehouse. (Apologies to DW people.)

    You could also store and represent a very large 32-bit color bitmap inside an Excel spreadsheet. Also supported are Base-64 Encoded BLOBs 🙂 :

    The total number of characters that can display in a cell
    Old Limit: 1k (when the text is formatted)
    New Limit: 32k or as many as will fit in the cell (regardless of formatting)

    The number of characters per cell that Excel can print
    Old Limit: 1k
    New Limit: 32k

    I’d hate to have to debug a 8k character formula with 64 levels of nesting:

    The maximum length of formulas (in characters)
    Old Limit: 1k characters
    New Limit: 8k characters

    The number of levels of nesting that Excel allows in formulas
    Old Limit: 7
    New Limit: 64

    A 255 argument function. The mind shudders. Even if you did not use a computer science major to create the program, such an unwieldly program might benefit from the education and experience of a computer science major. Maybe there is a better way to do this, by applying some mid-level software engineering techniques instead of stream of consciousness coding.

    Maximum number of arguments to a function
    Old Limit: 30
    New Limit: 255

     
c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel
%d bloggers like this: