none
Get-Windowsfeature Defaults

    Question

  • I'm looking to run get-windowsfeature to see all my IIS role services installed but I'd like to change the default installation values to say Default.  So for example in "Common HTTP features", "Static Content" gets installed by default so I'd like it to say "Static Content (Default)".  I can build the switch or the if then statement but if someone can just help me change the output I'd appreciate it.


    SMaximus7

    Monday, February 27, 2012 8:50 PM

Answers

  • Try this (I've tested it, and it works for me)...

    # Assumes 'defaults.txt' contains a list of the feature names 
    # as returned by Get-WindowsFeature.
    $defaults = Get-Content 'defaults.txt'
    filter CompareToDefaults { if ($defaults -contains $_) {"$_ (Default)"} else {$_}}
    Get-WindowsFeature | Where-Object { $_.installed } | select -ExpandProperty name | CompareToDefaults


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    • Marked as answer by SMaximus7 Tuesday, February 28, 2012 7:56 PM
    Tuesday, February 28, 2012 6:52 PM

All replies

  • Can you give an example of what you see and what you would like to see?  You may need to consider a ScriptProperty to parse each line in flight.
    Monday, February 27, 2012 10:56 PM
  • Hi,

    Thy below code, it will achieve all installed features:

    cls
    Get-WindowsFeature | where {$_.installed -eq $true} | select name, displayname, installed

    Best Regards,

    Yan Li


    Yan Li

    TechNet Community Support

    Tuesday, February 28, 2012 3:03 AM
    Moderator
  • This is what I'm currently using...

    import-module servermanager
    $RolFeat = get-windowsfeature | where-object {$_.installed -eq 'true'} | select displayname, parent,   featuretype
    if ($RolFeat -eq $null){$RolFeat = (gwmi win32_serverfeature -computername $target | select Name, ID, ParentID)}
    $RolFeat

    That way it will run on a 2008 and 2008R2 server.

    The reason behind this is that I do a security review on my servers before they are approved for production use and when I run the above script it gives me all installed features, however for IIS (The one I'm focusing on) certain role services are installed by default when you install IIS, When my report pops up I'd like to be able to easily identify which ones are installed by default and which ones aren't, instead of having to look it up all the time (I know after a while I'll just memorize it but it would also look good on the report).

    So for example instead of displaying "Static Content" it would display "Static Content (Default)"
    I tried doing...
    if ($rolfeat.displayname -eq "static content"){$rolfeat.displayname = "Static Content (Default)"}

    but that didn't do anything.

    SMaximus7

    Tuesday, February 28, 2012 1:21 PM
  • Perhaps a way to do this would be to run a check against a predefined array in the select cmdlet.  Below is an example of how to do the "Default" column with an array:

    $defaults = @('System32','System') # define what you anticipate to be "Default" (in this case I use C:\windows directories)
    dir C:\windows | select @{expression={if($defaults -match $_.name) {"Default"}};label="Default"},name


    Tuesday, February 28, 2012 4:50 PM
  • As far as I can see, there is no way of achieving what the OP wants, because the server does not store information about which role features were installed by default, and which were installed afterwards manually.

    A cludgy workaround would be to do a get-windowsfeature | export-clixml on a 'virgin' server, and then use that to compare with current installations.


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Tuesday, February 28, 2012 5:13 PM
  • No, I know that.  I would like info on creating a switch when I can say... if it says "Static Content" then instead display "Static Content (Default)"

    SMaximus7

    Tuesday, February 28, 2012 5:15 PM
  • Are you saying you have a list of features that you have identified as defaults?  I don't actually understand what you mean.  Where do you want to create the switch?   The cmdlet does not have this capability.

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Tuesday, February 28, 2012 5:22 PM
  • If you the string matches, then do replace.  Using dir name's for example:

    dir | % { $_ -replace $_, "$_ (Default)"}

    Or simpler, just append to the pipe:

    dir | % { "$_ (Default)" }

    • Edited by Will Steele Tuesday, February 28, 2012 5:23 PM extra comment
    Tuesday, February 28, 2012 5:22 PM
  • Yes, I have a list I've identified as default.  I would like to create the switch to say "go through the list of installed role services and if the role service is installed and it's name is "Static Content" then the name is "Static Content (Default), if the name is not defined in my switch then the name is the actual name.

    SMaximus7

    Tuesday, February 28, 2012 6:04 PM
  • Will, the problem is I can't get the string to match

    SMaximus7

    Tuesday, February 28, 2012 6:05 PM
  • Try this (I've tested it, and it works for me)...

    # Assumes 'defaults.txt' contains a list of the feature names 
    # as returned by Get-WindowsFeature.
    $defaults = Get-Content 'defaults.txt'
    filter CompareToDefaults { if ($defaults -contains $_) {"$_ (Default)"} else {$_}}
    Get-WindowsFeature | Where-Object { $_.installed } | select -ExpandProperty name | CompareToDefaults


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    • Marked as answer by SMaximus7 Tuesday, February 28, 2012 7:56 PM
    Tuesday, February 28, 2012 6:52 PM
  • Blasted exercising at lunch. : ) I was going to comment, as Bigteddy did, that you probably need the -ExpandProperty parameter pointing to name on the select.

    (Nice filter by the way BT.  Good practice putting learning to use.)

    • Edited by Will Steele Tuesday, February 28, 2012 6:57 PM extra comment
    Tuesday, February 28, 2012 6:56 PM
  • (Nice filter by the way BT.  Good practice putting learning to use.)


    I couldn't resist!  I just think they look cool.  Boe and mj are still debating the possible performance implications.

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Tuesday, February 28, 2012 7:05 PM
  • Yeah, I feel partly responsible for that. : ) First a typo, then, a bad test. Oh man...
    Tuesday, February 28, 2012 7:07 PM
  • Beautiful, and how do I throw in parent and featuretype?  I've actually tried to do this on different occasions and different scripts but when you do "select -expandproperty propertyname" then I can't add more objects


    SMaximus7

    Tuesday, February 28, 2012 7:30 PM
  • You said it.  But this does actually answer your original question.  "Throwing in parent and feature type" are not very easy, and would require a completely different script, because it's a different question.

    You should define the scope of your question up front, instead of what's known as "scope creep".  If we know what you want from the beginning, it affects design decisions.  Scope-creep is just frustrating.


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Tuesday, February 28, 2012 7:36 PM
  • I apologize, I did not mean to "scope-creep". 

    SMaximus7

    Tuesday, February 28, 2012 7:50 PM
  • I apologize, I did not mean to "scope-creep". 

    SMaximus7

    I understand that these ideas develop as you develop the script.   I suggest you start a new thread, stating all your objectives so far, and using this script as a starting point.

    As I said, it will need to be re-written, but at least posting it will show others what doesn't work for you, and make it easier for them to understand your requirements.

    All the best,

    BT.


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Wednesday, February 29, 2012 4:42 AM
  • Thank for your help BT I will do that

    SMaximus7

    Wednesday, February 29, 2012 1:07 PM