INTRODUCTION
Powershell methods, is there anything they can’t do? No, not really!
As with many other cases with Powershell, the options are endless.
In this blog post I will explain how you can use the Get-Content command in a Powershell script in an SCCM detection method.
MY SCENARIO
I came across a scenario at a customer where during an application installation, configuration files were to be replaced in the installation directory using a script. Since a configuration line was different in the different files, this had to be checked during the application detection after the installation.
The easy way out would have been to detect the existence of the configuration file, but I also wanted to make sure the content in the file was correct.
DETECTION METHODS
When configuring Detection Methods, you want the Detection Method to be as granular as possible.
Some out-of-the-box Detection Methods which work very well:
- MSI
- Registry keys
- File version
Detection Methods are one of the main culprits when dealing with application deployment issues.
Sometimes the Detection Method is simply misconfigured and the SCCM client does not detect the application as installed, or a Detection Method is configured as non-granular.
An example of this is adding to a script that a text file is to be created and then use this text file for detecting if the application is installed. This is NOT a good way of configuring Detection Methods.
Read more about Powershell Detection Methods here: Microsoft Docs – Introduction to Application Management
POWERSHELL DETECTION METHODS
HOW THEY WORK
When creating Powershell Detection Methods in SCCM, you need to understatnd how they work. This includes understanding how the output of your Powershell Detection Method is intepreted by SCCM.
The below table from Microsoft Docs shows how script output defines the result:
Script exit code | Data read from STDOUT | Data read from STDERR | Script result | Application detection state |
---|---|---|---|---|
0 | Empty | Empty | Success | Not installed |
0 | Empty | Not empty | Failure | Unknown |
0 | Not empty | Empty | Success | Installed |
0 | Not empty | Not empty | Success | Installed |
Non-zero value | Empty | Empty | Failure | Unknown |
Non-zero value | Empty | Not empty | Failure | Unknown |
Non-zero value | Not empty | Empty | Failure | Unknown |
Non-zero value | Not empty | Not empty | Failure | Unknown |
SOLUTION
DESCRIPTION
This solution uses Powershell to parse a text file, which in my example is a configuration file. It will then return a result if the string is found.
You can use this solution as an example if you want to look for other strings in text files.
HOW-TO
STEP 1 – CREATE APPLICATION
Start by going to the Software Library and press Create Application.

Select Manually specify the application information.

Enter the information of the application.

Input any additional information about the application.

STEP 2 – CREATE DEPLOYMENT TYPE
On the next step, press Add to add a new Deployment Type.

Select Manually specify the deployment type information.

Enter the Nameof the Deployment Type. This can be anything.

Enter the installation instructions of the application according to your requirements.

STEP 3 – CREATE DETECTION METHOD
Now we are the main subject of this blog post, namely the Powershell Detection Method.
Select Use a custom script to detect the presence of the deployment type. Press Edit.

MODIFY THE SCRIPT
Modify the below script to suit your needs. The example below will look strings in 3 different files. You will need to make the following modifications:
- String variables
- String<number>Location variables
You can of course also add additional checks as well as remove any as needed.
1 |
#Define strings and their location. Also include the filename.<br><br>$String1 = ""<br>$String1Location = ""<br><br>$String2 = ""<br>$String2Location = ""<br><br>$String3 = ""<br>$String3Location = ""<br><br># Detect presence of String1 in String1 Location<br>try {<br> $String1Exists = Get-Content $String1Location -ErrorAction Stop<br>}<br>catch {<br>}<br><br># Detect presence of String2 in String2 Location<br>try {<br> $String2Exists = Get-Content $String2Location -ErrorAction Stop<br>}<br>catch {<br>}<br><br># Detect presence of String3 in String3 Location<br>try {<br> $String3Exists = Get-Content $String3Location -ErrorAction Stop<br>}<br>catch {<br>}<br><br>if (($String1Exists -match $String1) -and ($String2Exists -match $String2) -and ($String3Exists -match $String3)) {<br> Write-Host "Installed"<br>}<br>else {<br>} |
Change Script type to Powershell and enter the code below this screenshot in the text box. You will of course need to modify it to suit your needs

STEP 4 – FINALIZE APPLICATION
Once you have added the Detection Method, you will need to go through the rest of the wizard to finalize the prcess.
STEP 5 – DISTRIBUTE & DEPLOY
End by Distributing and Deploying the application to users or devices, depending on your environment.
SUMMARY
Powershell Detection Methods are powerful to use when you cannot use the default MSI, registry or file version detections. They are however a bit more complex and getting your head around how the return logic of the script work might take some time.
For all scenarios when you can use a built-in Detection Method, use that, but in all other custom cases you can use Powershell Detection Methods.
REFERENCES
Popular posts
- How To Install .NET Framework 3.5 on Windows Server 2012-2016 and Windows 10 971,570 views
- Issue with mounting new ISO files from TechNet in Windows 8 and Windows 8.1 Preview 64,140 views
- Solve SCCM Error “sending with winhttp failed; 80072ee7” in Windows PE 32,466 views
- How To Enable PXE on Gen2 Hyper-V VMs for Legacy Boot 110,907 views
- Force Specific Sites to Always Run in Compatibility Mode Using GPO 146,238 views
- How to change between a Full Installation (GUI) and Server Core in Windows Server 2012 64,562 views
- Issue With DHCP and WDS on the Same Server 12,578 views
- Issue Starting Virtual Machine from ISO in Hyper-V 7,677 views
- BitLocker Status Check Using Powershell or the Command Line 121,272 views
- Use Diskpart to Create a Bootable Windows 10 USB 30,591 views
About the author
Daniel Classon works as a Senior Consultant at Mansoft, focusing on Microsoft Configuration Manager, Windows 10 and Powershell
OMG, how did I not know this?!?! https://t.co/EjKcxI9nkB
New Blog Post: Detect Strings in Text Files using Powershell Detection Methods in SCCM: https://t.co/IZNk0Q8tOi #ConfigMgr #SCCM
Deep, deep, DEEP dive into content distribution in #SCCM by the great @vinay_pamnani Internal-only content originally, now free for public use! https://t.co/oUPTSzrTmt