简体   繁体   中英

How to loop the For command to install windows 10 optional features powershell

Update:

So thanks to jw9364 I was able to get a working two liner done. However I have a long list of names to use so I attempted to go off his post and create a multi line loop that takes each name in the array and turns it into an object so we can download all packages in the list without throwing errors.

So as far as I can tell you need to get the output of a multiline array object to look like the two liner when it is called so

string1
string2

needs to look like

"string1","string2"

or in my specific case

"NetFx3","Netfx4-AdvSrvs"

when it is called by

Enable-WindowsOptionalFeature -Online -FeatureName $Feature

so every call looks like this to PS

Enable-WindowsOptionalFeature -Online -FeatureName NetFx3
Enable-WindowsOptionalFeature -Online -FeatureName Netfx4-AdvSrvs

My current script which is not working well shows you what is output in the terminal (using Write-Host to debug before changing to actual needed code)

I have had no luck getting the appropriate output. The closest I've come was getting the first line of text in the array to work correctly but any lines after that only produce the actual text string and do not include the Enable-WindowsOptionalFeature -Online -FeatureName which leads to the sought command failing.

Below is my latest script:

$FeatureName = @"
NetFx3
NetFx4-AdvSrvs
"@

$FeatureJoin = (-Join("$FeatureName," +$FeatureName))

ForEach($Feature in $FeatureJoin){
  Write-Host "Enable-WindowsOptionalFeature -Online -FeatureName $Feature"
}

This script produces the following output which obviously isn't going to work.

Enable-WindowsOptionalFeature -Online -FeatureName NetFx3
NetFx4-AdvSrvs,NetFx3
NetFx4-AdvSrvs

Original-Post:

I am new to powershell and have decided it's time to move on from cmd.exe (for some things).

I am trying to create a script to loop the For command in powershell to install a predefined list of all optional windows features that are not installed.

The list of features available for install is stored in the $FeatureName variable in the script itself as I don't want to store extra files to call from.

I want to loop each line in the list with this command (if this is the correct/best way to achieve my goal) ForEach-Object Enable-WindowsOptionalFeature -Online -FeatureName [FEATURE_NAME_HERE]

I am not sure at all if I am even close in my current script.

Here is what I have so far. Any help is appreciated.

$FeatureName = @"
ClientForNFS-Infrastructure
FaxServicesClientPackage
IIS-ASPNET45
"@ | Split-String -Separator "`r`n" | ForEach-Object {
Enable-WindowsOptionalFeature -Online -FeatureName [$_FeatureName.text]
}

here's a quick two line i drew up. You can make it one line if you really want, but its so much easier to read this way and very reusable.

$FeatureName = 'ClientForNFS-Infrastructure', 'FaxServicesClientPackage','IIS-ASPNET45'
foreach($feature in $FeatureName) {Enable-windowsoptionalfeature -online -featureName $feature}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM