Skip to main content

Copy a File to a Folder in a User's Profile

  • September 10, 2020
  • 5 replies
  • 677 views

Tony-Automox

I generated this worklet for a customer that wanted to take a text file called “signin.slacktoken” with a Slack token in it and have it copied to users’ Download directory. For your purposes, upload the file you want to be copied and set the $uploadfile variable in the evaluation and remediation to be exactly equal to the name of the uploaded file. Then just set the location you want the file in evaluation and remediation.


Evaluation:


# Set variable to the exact name of the installation file uploaded

$uploadfile = 'signin.slacktoken'



# Sets current user

$currentusr = (Get-WmiObject -class win32_process -ComputerName 'localhost' | Where-Object name -Match explorer).getowner().user



# Check to see if the file exists in the user's profile location where it should be saved.  If so, exit with no action.

if (Test-Path -Path "c:\Users\$currentusr\Downloads\$uploadfile") {

	exit 0

} else { Exit 1 }


Remediation:


# Sets current user

$currentusr = (Get-WmiObject -class win32_process -ComputerName 'localhost' | Where-Object name -Match explorer).getowner().user



# Set variables to the exact name of the installation file uploaded & where to copy it

$uploadfile = "signin.slacktoken"

$uploadPath = "c:\Users\$currentusr\Downloads"



# If directory doesn't exist, create it

If (!(Test-Path $uploadPath)) {

    New-Item -ItemType Directory -Path $uploadPath -Force

}



# Copy the installation file uploaded to the desired location in the user's profile.  In this example it's the user's Downloads folder.

Copy-Item $uploadfile -Destination "$uploadPath\$uploadfile"

5 replies

Forum|alt.badge.img
  • Pro
  • 49 replies
  • September 21, 2020

Thanks I’ll give this a try


Forum|alt.badge.img
  • Novice
  • 9 replies
  • July 13, 2021

Works great for me as long as the folder exists. I found that if the folder you specified does not exist the script will attempt to create the folder, but something causes the folder to be created as a file instead. This causes the operation to fail.


I corrected this by adding the following just above the copy-item operation


New-Item -ItemType File -Path "c:\some\path\filename.ext" -Force

Tony-Automox
  • Author
  • Automox Employee
  • 35 replies
  • July 19, 2021

Thanks for your input. I tweaked my code to handle non-existant folders. I wrote it for a known existing folder so wasn’t considering that.


Forum|alt.badge.img

This worked great! Thanks so much!


awesome, thank you so much.  been struggling to replace/add files in a users appdata/roaming folder until now.  


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings