2

I'm trying to set a property of for example an ApplicationPool with PowerShell (version 2).
(I've a Windows 7 64 bit machine if that matters)

I see that this example uses Set-ItemProperty and this example uses a dot . to set a property of an object / element:

$pool | Set-ItemProperty -Name "managedRuntimeVersion" -Value "v4.0"

versus:

$pool.managedRuntimeVersion = "v4.0"

So what's the difference? I think that the second one is much more readable, but I don't know what the implications are.

EDIT:
I noticed that (at least in this case) there is a difference, the Set-ItemProperty does save the value of the property directly, while the other method does set the value (while debugging) but does not save it afterwards. I've not found out yet why this happens. (Do you need to call something like save or commit?) See @moonstom's answer, for Powershell 2.0 Set-ItemProperty is the only way or $pool | Set-Item for Powershell 3+ (see sample).

Community
  • 1
  • 1
Kapé
  • 4,411
  • 3
  • 37
  • 54

2 Answers2

3

You're working on a representation of that app pool. If you check the type of that object, you'll get a configuration element. So after setting it up, you need to push your settings back with $pool | Set-Item, available in PS 3.0 and above. Otherwise your only alternative is Set-ItemProperty

MoonStom
  • 2,847
  • 1
  • 26
  • 21
  • Apparently I didn't have a link to a full sample. Now I [found one](http://www.iis.net/learn/manage/powershell/powershell-snap-in-making-simple-configuration-changes-to-web-sites-and-application-pools), see section _SET-ITEM AND GET-ITEM_. I'm using Powershell 2.0 so will have to use `Set-ItemProperty` then. – Kapé Apr 01 '15 at 21:19
-2

There is no difference. In the first one you pass the object to the Set-ItemProperty commandlet via the pipe and the commandlet setting the object property.

The second one you're setting it directly on the object. But they are functionally the same. With the second one you could also retrieve the property's value like this:

$value = $pool.managedRuntimeVersion
Nathan Rice
  • 3,091
  • 1
  • 20
  • 30
  • Thanks for your clean answer. However, I noticed that setting directly on the object does not save the value? [See sample code](https://gist.github.com/kpstolk/604120c5aaf6699d19ab). Are you sure that there's no difference or am I doing anything wrong here? – Kapé Mar 13 '15 at 13:19
  • That's what's happening to me too. Setting the pool via the dot notation does nothing. – MoonStom Apr 01 '15 at 20:18