vssadmin - how to get the Shadow ID & Volume Name as variables?

Hi,

I am trying to use vssadmin.exe within a Windows 2012 R2 server Powershell script, but I cannot workout how to get the Shadow ID & Volume Name as variables?

vssadmin create shadow /for=C:

vssadmin 1.1 - Volume Shadow Copy Service administrative command-line tool
© Copyright 2001-2013 Microsoft Corp.

Successfully created shadow copy for ‘C:’
Shadow Copy ID: {b01a4dab-61b0-4cfb-908c-94278500b10d}
Shadow Copy Volume Name: \?\GLOBALROOT\Device\HarddiskVolumeShadowCopy853

Any help will be appreciated.

Thanks
M

As it’s a script, I guess it doesn’t matter if you don’t see output for vssadmin?

Select the two strings from the command’s output and assign them to a variable.
Split the strings and select the second element, trim to remove white space.

$string = vssadmin create shadow /for=C: | Select-String 'Shadow Copy ID','Shadow Copy Volume'

$id = ($string[0] -split 'ID:')[1].trim()

$Volume = ($string[1] -split 'Name:')[1].trim()

Write-Output "ID = $id, Volume = $Volume"

This only handles your example, if you were using list shadows you’d need a different technique as you’ll probably have multiple IDs and volume names.

Thank you Matt, that is exactly what I needed.

Without great people like yourself, I would be completely lost with PowerShell.!!

Thank you again.

M