# Getting supported sleep states

**URL:** <https://forums.powershell.org/t/getting-supported-sleep-states/17633>\
**Category:** PowerShell Help\
**Created:** [September 25, 2021, 12:11pm UTC](https://forums.powershell.org/t/getting-supported-sleep-states/17633 "2021-09-25T12:11:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jackonda](https://avatars.discourse-cdn.com/v4/letter/j/b5e925/32.png) [@jackonda](https://forums.powershell.org/u/jackonda)\
**Post date:** [September 25, 2021, 12:11pm UTC](https://forums.powershell.org/t/getting-supported-sleep-states/17633/1 "2021-09-25T12:11:38Z")

</div>

Hello everyone,

I’m trying to create a script to list all available sleep states for a device, so I’m running ‘powercfg /a’ and I tried to create a regex expression to extract the lines that interest me and it seems to show what I need → [regex101: build, test, and debug regex](https://regex101.com/r/VPdWTy/1)

But when I run the below code, I don’t get any result - what am I doing wrong here 🙂 ?  
…  
$SleepStates = powercfg /a  
$RegexSleepStates = “(?smi)((?\<=The following sleep states are available on this system:\n)._?$.+?^\n_?$)”  
select-string -inputobject $SleepStates -Pattern $RegexSleepStates -AllMatches | ForEach-Object { $_.Matches } | ForEach-Object { $_.Value }  
…

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [September 25, 2021, 4:09pm UTC](https://forums.powershell.org/t/getting-supported-sleep-states/17633/2 "2021-09-25T16:09:13Z")

</div>

Hi Karol,

First issue I see if you’re trying to span multiple lines when the input is going to be line by line. Typically you would just make it a single string value rather than an array of strings, but I had weirdness with the output. Probably something to do with the way powershell reencodes the output. So instead of helping fix your attempt, I offer an alternative solution.

```auto
$capture = $false

switch -Regex (powercfg /a){
    'The following sleep states are available on this system:' {
        $capture = $true
    }

    '^\s{2,4}(.+$)' {
        if($capture){
            $matches.1
        }
    }

    'The following sleep states are not available on this system:' {$capture = $false}
}

```

Please give it a try while waiting for others to offer input. If you want to capture the output to a variable just put the variable assignment before the switch statement.

```auto
$capture = $false

$MyVariable = switch -Regex (powercfg /a){
    'The following sleep states are available on this system:' {
        $capture = $true
    }

    '^\s{2,4}(.+$)' {
        if($capture){
            $matches.1
        }
    }

    'The following sleep states are not available on this system:' {$capture = $false}
}

```

---

<div class="post-metadata">

**Author:** ![jackonda](https://avatars.discourse-cdn.com/v4/letter/j/b5e925/32.png) [@jackonda](https://forums.powershell.org/u/jackonda)\
**Post date:** [September 27, 2021, 5:54pm UTC](https://forums.powershell.org/t/getting-supported-sleep-states/17633/3 "2021-09-27T17:54:40Z")

</div>

Hi Doug,

thank you very much for your help!  
Your code does exactly what I needed 🙂  
The construct you proposed is quite complicated for my current PS/Regex skills, but I’ll go over it a couple of times until I understand it.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:21pm UTC](https://forums.powershell.org/t/getting-supported-sleep-states/17633/4 "2024-05-16T20:21:17Z")

</div>

Closed on May 16, 2024, 8:21pm UTC


