Copy a OVPN file in Powershell from Intune

Copper Contributor

Sounds easy, my powershell script works fine if i run it on my machine, it copies the file into the %USERPROFILE%\OpenVPN\config folder. Its packaged up using the Intunewinapp program, but when i put it into intune it fails to install and copy the file over. Culd something like defender be stopping this process? The script works, the intune package works when launched from powershell, so what causes it to fail when i run it as a win32 app from intune? Anyone had this issue before?


This is my script;

$PSScriptRoot = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
$Source = "$PSScriptRoot\Thornes.ovpn"
$Destination = "$env:USERPROFILE\OpenVPN\config\"

Copy-Item -Path $Source -Destination $Destination -Recurse -Force



16 Replies
Hi... %userprofile% ... I am missing how the install behavior is configured
Is it configured as System?

If run as system, maybe searching the logged in user first? and change the destination to match the username who has explorer running?

$username = (get-process -name "explorer" -includeusername).username
$username = ($username -split '\\')[1]

$Destination = c:\users\$username\OpenVPN\config\"


Sorry, yes, its configured to run as system, if i run it on my machine locally it does what it should be. ill look at your suggestions though and see if i can get it working thanks.

Guess where the %userprofile% resolves to when deploying it as system : C:\Windows\System32\Config\systemprofile :)


and you could always use wildcards in the copy process c:\users\*\openvpn

i did have the first one configured as user, that got the same result, but ill redo it again, maybe there was another reason that it didnt run.

@Rudy_Ooms_MVP  Dam, tried it again as user, still fails from intune, doesnt copy the file into the open VPN config folder,  will make some futher amendments!

Hi Chris,

Have you tried to use this C:\Users\%USERNAME%\openvpn\…?

This should do it if you run it as Run As User (Install Behavior).


for some reason no, is this whats stopping endpoint from writing the file? Ill try it though
So, if i replace $env:USERPROFILE\OpenVPN\config\ with C:\Users\%USERNAME%\openvpn\config\ it wont run in powershell, runs ok from the run command but powershell doesnt like that format
What happens when using a * wildcard instead of the username variable?
No luck, im wondering if theres something else at play here on the client machine. Can endpoint protection stop these powershell scripts from running?
From being deployed by endpoint i mean, because it runs fine if i run it on the local machine. its just the remote installation that fails

Hi Chris,

You may need to change you powershell script to the one below, I have test it on my machine now.

$Path = New-item -ItemType directory -force -path "c:\users\%username%\OpenVPN\config\"
Copy-item -path $Source -Destination $path -Recurse -Force




My test has not work, I will update you soon

@Moe_Kinani  Ill try that Moe, ive just thought sod it and pushed it out to all users, for some reason it says its installed on a couple of machines and failed on 10, so im wondering if its something on my machine, which ive been  mostly using for testing.  ive attached an Intune logfile if that helps anyone, just looking at it myself now.  

Got it, , my test has not worked either. I will update the thread with my test soon.


@Moe_Kinani  ah right, yea, it didnt work when i tried thee path like that either. So,  here is the current install so far, out of 14 machines its only run on 2. So i think its at the client side. All the machines are domain joined and from the same image. All users are just standard users, but there must be something client side i think. The script fundamentally does work. Attached an image of the install status 

Hi Chris,

Can you adjust your script similar to the one below? I have tested it and worked in my test machine.


#Get only the copied file
$copiedfile = Get-ChildItem $PSScriptRoot -File

#Get all Profiles with Exclude basic Profiles
$Excluded_Profiles = @( 'Default User', 'All Users', 'Default', 'Public' )
$Profiles = Get-ChildItem 'c:\users\' -Directory -force | Where-Object { $_.BaseName -notin $excluded_profiles }

#Get Folder Path to place the File
$targetFolder = "\OpenVPN\config”

foreach ($ProfilePath in $Profiles.FullName) {
$Destination = Join-Path -Path $ProfilePath -ChildPath $TargetFolder

try {
if (-not (Test-Path $Destination -ErrorAction Stop)) {
New-Item -Path $Destination -Itemtype Directory -ErrorAction Stop
Copy-Item -Path $copiedfile -Destination $Destination -Force -ErrorAction Stop
} catch {}