so i can write bcd commands in a powershell script as if i were in a cmd prompt, for example:
bcdedit /default '{current}'
however i need a script that does this:
bcdedit /default '{current}'
bcdedit /set '{otherboot}' description "my description"
if it didnt do that, it would be the other way around:
bcdedit /default '{otherboot}'
bcdedit /set '{current}' description "my description"
what i need to do is find the identifier of the other boot in powershell and im not sure how. all google searches say to do this:
$bcdStore=gwmi -name root\wmi -list bcdstore -enableall
$bcdStore|gm
$result=$bcdStore.OpenStore("") # can also use explicit file name.
$store=$result.Store
but i have no idea how to use the store once i have it and this seems a little too complicated. i mean there should be an easier way... no?
I don't know of a way to do it with WMI, but you could use
bcdeditin combination withSelect-String:Explanation:
The output of
bcdedit /enumlooks roughly like this:The relevant sections of this output are the
Windows Boot Loadersections, which – unlike theWindows Boot Managersection – have apathrecord. Thus we can use this record to select only theWindows Boot Loadersections:Since the
identifierrecords are 2 lines before thepathrecords, we need 2 lines ofPreContext(and noPostContext):Now we have selected the follwing two chunks from the output of
bcdedit /enum:identifier {current} device partition=C: path \Windows\system32\winload.exeidentifier {e0610d98-e116-11e1-8aa3-e57ee342122d} device partition=C: path \Windows\system32\winload.exeSince we're only interested in the first line of the
PreContextwe select these 2 lines using aForEach-Objectloop:which reduces the two chunks to this:
identifier {current}identifier {e0610d98-e116-11e1-8aa3-e57ee342122d}from which we remove the category (
identifier) via string replacement:The regular expression
'^identifier +'matches a (sub)string starting with the word "identifier" followed by one or more spaces, which is replaced with the empty string. After this replacement the two chunks are reduced to this:{current}{e0610d98-e116-11e1-8aa3-e57ee342122d}So now we only need to filter out the chunk containing
{current}and what's left is the identifier of the other boot record:After this, the variable
$otherbootcontains the identifier of the not-current boot record.