Skip to content

Instantly share code, notes, and snippets.

@daemondevin
Last active November 2, 2025 02:33
Show Gist options
  • Select an option

  • Save daemondevin/f0fc29e157d207243dbcd6b916ba83d6 to your computer and use it in GitHub Desktop.

Select an option

Save daemondevin/f0fc29e157d207243dbcd6b916ba83d6 to your computer and use it in GitHub Desktop.
NSIS macros for checking and enabling Windows Features
; Check if Windows Feature needs to be enabled (e.g., .NET 3.5 on Windows 10+)
!define CheckWindowsFeature `!insertmacro _CheckWindowsFeature`
!macro _CheckWindowsFeature _FEATURE _RESULT
Push $0
Push $1
Push $R8
StrCpy ${_RESULT} "unknown"
; Use DISM to check Windows feature status
ExecWait 'dism /online /get-featureinfo /featurename:"${_FEATURE}"' $0
${Switch} $0
${Case} "0"
; Feature command succeeded, need to parse output
StrCpy ${_RESULT} "available"
${Break}
${Case} "87"
; Feature not found
StrCpy ${_RESULT} "not_available"
${Break}
${Default}
; Other error
StrCpy ${_RESULT} "error"
${Break}
${EndSwitch}
Pop $R8
Pop $1
Pop $0
!macroend
; Enable Windows Feature
!define EnableWindowsFeature `!insertmacro _EnableWindowsFeature`
!macro _EnableWindowsFeature _FEATURE _RESULT _ERROR
Push $0
Push $1
StrCpy ${_RESULT} "false"
StrCpy ${_ERROR} ""
${DebugMsg} "Enabling Windows feature: ${_FEATURE}"
ExecWait 'dism /online /enable-feature /featurename:"${_FEATURE}" /all /norestart' $0
${Switch} $0
${Case} "0"
StrCpy ${_RESULT} "true"
${DebugMsg} "Windows feature enabled successfully: ${_FEATURE}"
${Break}
${Case} "3010"
StrCpy ${_RESULT} "true"
StrCpy ${_ERROR} "Feature enabled, restart required"
${DebugMsg} "Windows feature enabled, restart required: ${_FEATURE}"
${Break}
${Default}
StrCpy ${_ERROR} "Failed to enable feature, error code: $0"
${DebugMsg} "Failed to enable Windows feature ${_FEATURE}: $0"
${Break}
${EndSwitch}
Pop $1
Pop $0
!macroend
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment