2

So I have an item's name defined as $name, and a table/object of items called $x. That looks like

Name       id       PendingShutdown
____       ___      ___________
example j-12453634 False

my variable $name = "example"

What I want to do is take $name, and change $x so that it looks like

Name       id       PendingShutdown
____       ___      ___________
example j-12453634 True

How would I go about doing this?

Montel Edwards
  • 400
  • 5
  • 14

2 Answers2

2

if PendingShutdown is a boolean

 $x | where Name -eq "example" | %{$_.PendingShutdown=$true}

if PendingShutdown is a string

$x | where Name -eq "example" | %{$_.PendingShutdown="True"}
Esperento57
  • 16,521
  • 3
  • 39
  • 45
0

You can select the requested item via Where-Object (or ? for short) by using a pipe and then use $name as part of the query. Keep in mind that the result could return multiple items if you have duplicates in the Name property (in that case you can use | Select -First 1 after the first pipe).

PS > $name = "example"

# setup of list items
PS > $x = @{}
PS > $x.Name = "example"
PS > $x.id = "j-12453634"
PS > $x.PendingShutdown = $false;

PS > $x2 = @{}
PS > $x2.Name = "other-name"
PS > $x2.id = 42
PS > $x2.PendingShutdown = $false

PS > $items = @()
PS > $items += $x
PS > $items += $x2

# displaying list before modification
PS > $items

Name                           Value
----                           -----
PendingShutdown                False
Name                           example
id                             j-12453634
PendingShutdown                False
Name                           other-name
id                             42

# selecting item from list via Name property with value from variable
PS > $items |? Name -eq $name

Name                           Value
----                           -----
PendingShutdown                False
Name                           example
id                             j-12453634

# updating item via selector from variable
PS > ($items |? Name -eq $name).PendingShutdown = $true

# displaying list after modification
PS > $items

Name                           Value
----                           -----
PendingShutdown                True
Name                           example
id                             j-12453634
PendingShutdown                False
Name                           other-name
id                             42
Ronald Rink 'd-fens'
  • 1,289
  • 1
  • 10
  • 27