# Uninstall Forticlient any version through Intune

**URL:** <https://forums.powershell.org/t/uninstall-forticlient-any-version-through-intune/23158>\
**Category:** PowerShell Help\
**Created:** [November 15, 2023, 7:36pm UTC](https://forums.powershell.org/t/uninstall-forticlient-any-version-through-intune/23158 "2023-11-15T19:36:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Francis](https://avatars.discourse-cdn.com/v4/letter/j/97f17d/32.png) [@John\_Francis](https://forums.powershell.org/u/John_Francis)\
**Post date:** [November 15, 2023, 7:36pm UTC](https://forums.powershell.org/t/uninstall-forticlient-any-version-through-intune/23158/1 "2023-11-15T19:36:45Z")

</div>

This is the first script, so I then wanted to modify this so that I can write any errors in a text file to log it so can look into the log and see what is causing the issue, but when I run the below script it give me error shown below.  
The uninstall worked fine with my script but it is showing as Error in Intune Overview

```auto
$forticlient = Get-WmiObject Win32_Product | Where-Object {$_.Name -like "*FortiClient*"}
msiexec /uninstall $forticlient.IdentifyingNumber /quiet /norestart

```

```auto
# Check for administrator rights
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
    Write-Host "Please run this script as an administrator."
    exit
}

# Define the name of the FortiClient executable
$fortiClientExecutable = "FortiSSLVPNClient.exe"

# Get the list of installed FortiClient versions
$fortiClientVersions = Get-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\* -ErrorAction SilentlyContinue |
    Where-Object { $_.DisplayName -like "FortiClient VPN*" } |
    ForEach-Object { $_.DisplayName }

# Uninstall FortiClient versions
foreach ($version in $fortiClientVersions) {
    $uninstallArgs = "/x $($_.PSChildName) /quiet /norestart"
    Write-Host "Uninstalling $version with arguments: $uninstallArgs"
    
    try {
        # Attempt to uninstall FortiClient
        Start-Process -FilePath "msiexec.exe" -ArgumentList $uninstallArgs -Wait -ErrorAction Stop
    }
    catch {
        # Capture error details and write to a text file
        $errorMessage = "Error uninstalling $version:`n$($_.Exception.Message)"
        $errorFilePath = "C:\temp\ForticlientError.txt"
        Add-Content -Path $errorFilePath -Value $errorMessage

        Write-Host "Error details written to $errorFilePath"
    }
}

Write-Host "FortiClient VPN Client uninstallation completed."

```

Get the following error - How can I fix it?

At line:26 char:45

- 

```
    $errorMessage = "Error uninstalling $version:`n$($_.Exception ...

```

- 

```
                                        ~~~~~~~~~

```

Variable reference is not valid. ‘:’ was not followed by a valid variable name character. Consider using ${} to delimit the name.  
+ CategoryInfo : ParserError: (🙂 , ParentContainsErrorRecordException  
+ FullyQualifiedErrorId : InvalidVariableReferenceWithDrive

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [November 15, 2023, 7:49pm UTC](https://forums.powershell.org/t/uninstall-forticlient-any-version-through-intune/23158/2 "2023-11-15T19:49:12Z")

</div>

> [@John\_Francis](#):
>
> ` $errorMessage = "Error uninstalling $version:`n$($\_.Exception.Message)"`

→

```auto
$errorMessage = "Error uninstalling $($version):`n$($_.Exception.Message)"

```

And again …

1. You should not use `Get-WmiObject` anymore as it is depricated. Use `Get-CimInstance` instead.
2. Using the `Win32_Product` class is usually a bad idea and might not do what you think it does or sometimes it does even more than you want.  
[https://xkln.net/blog/please-stop-using-win32product-to-find-installed-software-alternatives-inside/](https://xkln.net/blog/please-stop-using-win32product-to-find-installed-software-alternatives-inside/)

> [@John\_Francis](#):
>
> ```auto
> foreach ($version in $fortiClientVersions) {
> $uninstallArgs = "/x $($_.PSChildName) /quiet /norestart"
> Write-Host "Uninstalling $version with arguments: $uninstallArgs"
> ...
> 
> ```

You’re using the pipeline variable `$_` but you actually don’t have a pipeline.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [February 7, 2024, 6:25am UTC](https://forums.powershell.org/t/uninstall-forticlient-any-version-through-intune/23158/3 "2024-02-07T06:25:22Z")

</div>

Closed on February 7, 2024, 6:25am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
