[CmdletBinding(SupportsShouldProcess = $true)] param( [string]$Volume = 'C:\', [switch]$ListOnly ) $ErrorActionPreference = 'Stop' $identity = [System.Security.Principal.WindowsIdentity]::GetCurrent() $principal = New-Object System.Security.Principal.WindowsPrincipal($identity) if (-not $principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)) { throw 'Run PowerShell as Administrator.' } if ($Volume -notmatch '^[A-Za-z]:\\?$') { throw 'Volume must look like C:\' } $volumeRoot = $Volume.Substring(0, 2) + '\' $existing = @(Get-CimInstance -ClassName Win32_ShadowCopy -ErrorAction SilentlyContinue) if ($existing.Count -gt 0) { $existing | Select-Object ID, InstallDate, VolumeName | Format-Table -AutoSize } else { Write-Host 'No shadow copies found.' } if ($ListOnly) { return } if (-not $PSCmdlet.ShouldProcess($volumeRoot, 'Create VSS snapshot')) { return } $class = [wmiclass]'root\cimv2:Win32_ShadowCopy' $result = $class.Create($volumeRoot, 'ClientAccessible') if ($result.ReturnValue -ne 0) { throw "Shadow copy failed with code $($result.ReturnValue)" } Write-Host "Shadow copy created: $($result.ShadowID)" Write-Host "Volume: $volumeRoot" Write-Host 'Remove it later with vssadmin when it is no longer needed.'