PADT – Files and Folders

PowerShell Application Deployment Toolkit is a tool very powerful to manage files and folders:

  • Check if a file exists
  • Copy or move a single file or a group of files (from the package or from the system)
  • Copy some files recursively in all existing user profiles
  • Create, copy or deletes folders, sub-folders and content

1. Files

1.1 Check if a File Exists

## Check if a file exists
Write-Log "----- Check if the file C:\Workdir\MyFile.exe exists -----"
If (Test-Path 'C:\Workdir\MyFile.exe') {
Write-Host 'The file exists'
}
Else {
Write-Host 'The file does not exists'
} 

 

1.2 Single File Copy

## Single File Copy (from system)
Write-Log "----- Copy the file C:\Program Files\MyApp\MyApp.ini in C:\Backup -----"
Copy-File -Path 'C:\Program Files\MyApp\MyApp.ini' -Destination 'C:\Backup\MyApp.ini'
 
 
## Single File Copy (from package)
Write-Log "----- Copy the file MyApp.ini in C:\Backup -----"
Copy-File -Path "$dirFiles\MyApp.ini" -Destination 'C:\Backup\MyApp.ini'

 

1.3 All Files Copy

## All Files Copy
Write-Log "----- Copy all the files of C:\Program Files\MyApp in C:\Backup -----"
Copy-File -Path 'C:\Program Files\MyApp\*.*' -Destination 'C:\Backup'

 

1.4 Copy Files in All Existing User Profiles

## Copy file in all existing user profiles
$ProfilePaths = Get-UserProfiles | Select-Object -ExpandProperty 'ProfilePath'
ForEach ($Profile in $ProfilePaths)
{
     Write-Log -Message "-------------- Copying files to $Profile --------------" 
Copy-File -Path "$dirFiles\MyFile.txt" -Destination "$Profile\AppData\Roaming\Editor\Application\MyFile.txt" -ContinueOnError $true               
} 

 

2. Folders

2.1 Create Folder

## Create folder
Write-Log "----- Creating the folder My_New_Folder -----"
New-Folder -Path "$envWinDir\System32\My_New_Folder"

 

2.2 Copy Folder

## Folder and subfolders copy
Write-Log "----- Copy the folder C:\Program Files\MyApp in C:\Backup -----"
Copy-Item 'C:\Program Files\MyApp' 'C:\Backup\MyApp' -recurse

 

2.3 Delete Folder

## Delete folder and its content
Write-Log "----- Deleting the folder My_Folder and its content -----"
Remove-Folder -Path "$envWinDir\Temp\My_Folder"