How To Windows Commands Exit with the Wrong Code

Windows commands often exit with a 0 code, even if the command fails. Catch errors like this using Select-String; the Powershell equivalent of grep.

1echo "Attempting to sync Group Policy..."
2$out = gpupdate
3echo $out
4
5$result = Select-String -InputObject $out -Pattern "error"
6if ($result.length -ne 0) {
7    exit 1
8}