Preface: I have not been formally trained on how to code. I have kind of winged it and done simple thing like registry entries via powershell.
I have a small script I am trying to run that will test to see how many Virtual Desktops I have running (Get-DesktopCount). I want that return to be tested and if there are 2 or more Virtual Desktops then execute the second part of the statement which is to delete any virtual desktops until there are only two virtual desktops left (Remove-Desktop)
$DTC = Get-DesktopCount
Do
{
Remove-Desktop -Desktop "Desktop 2"
}
Until (($DTC) -gt '2')
I know it has something to do with Syntax and me not being able to distinguish the different types of returns for different cmdlets.
I see three things here
The first is that when you do $DTC = Get-DesktopCount
, what happens is you're saving the result of Get-DesktopCount
as $DTC
. No matter how many desktops you create or remove, the value stored in $DTC
has already been set and will not update until you tell it to change.
The second is that the loop's condition seems to be backwards - you probably want to keep going Until
there are less than 2 desktops (or, While
there are more)
Finally, while I don't think it does any harm in this case, I don't think you want to use Do
here - by using Do { ... } While ( ... )
instead of While ( ... ) { ... }
you end up running the block at least once, even if the condition is false (so you close Desktop 2 at least once, even if there are fewer than 2 desktops)
All in all, I think what you want is probably something closer to
While ( (Get-DesktopCount) -gt 2 ) {
Remove-Desktop -Desktop "Desktop 2"
}