Forum Discussion

Keke_000's avatar
Keke_000
Copper Contributor
Nov 28, 2023

copying files to an existing folder

i have a script to detect if a folder existence. I am trying to add to my script of the folder exist just copy the files or how can i have it overwrite the existing folder? Please any guidance is appreciated. I've included my script incase it was needed.

 

p.s still a newbie at this

 

Start-Transcript
# Check Folder Exists
If (Test-Path -Path $env:APPDATA\Microsoft\Templates\Charts) {
#Get the Folder object
$Folder = Get-Item -Path $env:APPDATA\Microsoft\Templates\Charts
Write-Host "Folder already exists." -f Yellow
} Else {
#Create a New Folder
$Folder = New-Item -ItemType Directory -Path $env:APPDATA\Microsoft\Templates\Charts
Write-Host "Folder Created Successfully!." -f Green


#gets currently logged in user
$Username = ((Get-WMIObject -ClassName Win32_ComputerSystem).Username).Split('\')[1]

 


New-Item -Path "C:" -Name "Charts" -ItemType "directory" -Force
Copy-Item -Path "Charts\*" -Destination "C:\Users\$Username\AppData\Roaming\Microsoft\Templates\Charts" -Recurse -Force
}
Stop-Transcript

  • Hello Keke_000 

     

    Welcome to the Microsoft community, my name is Recep I'll be happy to help you today.

     

    It seems like you want to check if a folder exists, and if it does, copy the contents of another folder into it. If it doesn't exist, you create the folder and then copy the contents. Also, you've mentioned that you want to overwrite the existing folder if it's already there.

     

    Here's an updated version of your script:

     

    Start-Transcript

     

    # Define the source and destination paths

    $sourcePath = "C:\Charts"

    $destinationPath = "$env:APPDATA\Microsoft\Templates\Charts"

     

    # Check if the folder exists

    if (Test-Path -Path $destinationPath) {

        Write-Host "Folder already exists." -ForegroundColor Yellow

       

        # Remove existing destination folder and its content

        Remove-Item -Path $destinationPath -Recurse -Force

    }

     

    # Create a new folder

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

    Write-Host "Folder created successfully." -ForegroundColor Green

     

    # Copy files from source to destination

    Copy-Item -Path "$sourcePath\*" -Destination $destinationPath -Recurse -Force

     

    Stop-Transcript

     

     

    If I have answered your question, please mark your post as Solved

    If you like my response, please give it a Like :smile:

    Appreciate your Kudos! Proud to contribute! 🙂

     

    • Keke_000's avatar
      Keke_000
      Copper Contributor
      I am probably complicating it. the goal is to deploy the folder and its content via intune.
      I need to create a folder with its content if it doesnt exist but if it does exist i need it to mirror the folder with it content that i am providing. The script does work when i run it locally. I intune wrap everything and do the deployment but intune is throwing an error that it has installed but cannot locate the file. this is not true since the test machines i tested with did not get the updated files.
      • Deleted's avatar
        Deleted

        Hello Keke_000 

         

        Welcome to the Microsoft community, my name is Recep I'll be happy to help you today.

         

        • Ensure your script logs its activities properly, especially if it's running as a background process. You can redirect output using Out-File or Start-Transcript to capture any errors or debug information.

        Start-Transcript -Path "C:\Path\To\Your\LogFile.txt" –Append

        • Ensure that the execution policy allows the script to run. You can set the execution policy within the script if needed.

        Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass

        • Verify that the account running the script (usually SYSTEM when deployed by Intune) has the necessary permissions to create folders and copy files.
        • Set the working directory explicitly at the beginning of your script to avoid any confusion about where the script is running.

        Set-Location -Path "C:\Path\To\Your\Script"

        • Provide absolute paths for both source and destination. When running as a system process, the working directory might not be what you expect.
        • When copying files, provide the full paths. Intune may not resolve relative paths correctly.
        • If you're using environment variables, make sure they are resolved correctly when the script runs in the Intune environment.
        • Ensure that your Intune deployment configuration specifies the correct script to run and any required parameters.
        • For troubleshooting Intune-specific issues, check the Intune Management Extension logs on the target machine. These logs can be found in C:\ProgramData\Microsoft\IntuneManagementExtension\Logs.
        • Consider adding a short delay at the beginning of your script to allow the Intune Management Extension to initialize.

        Start-Sleep -Seconds 30

         

        If I have answered your question, please mark your post as Solved

        If you like my response, please give it a Like :smile:

        Appreciate your Kudos! Proud to contribute! 🙂

         

Resources