I need assistance with a simple PowerShell script. I'm only a beginner with this language, so forgive me for any mistakes in language.
I need a script to copy a file (which is in the same directory as the PowerShell script) into every user directory on a PC when executed, minus a few. This is assumed for Windows 7 folder structures.
Code Idea:
- Parse top level subdirectories under C:\Users
- Copy an item named deployment.properties to C:\Users\USER_PROFILE_NAME\AppData\LocalLow\Sun\Java\Deployment\ - overwriting the file if it exists and creating the folder structure if it is not present
- Do not do this for All Users, Administrator, and Default accounts
I had a script of sorts that parsed the usernames, but when it was run it added @ in from of each username. It also failed to create subdirectories if they didn't exist. I feel like this is a lot more simple than I'm making it out to be.
This is what I've got for my code so far:
$UserFolders = get-childitem -path "C:\Users" | ?{$_.Psiscontainer} |select-object fullname
$from = ".\deployment.properties"
foreach ($UserFolder in $Userfolders)
$to = "C:\Users\$UserFolder\AppData\LocalLow\Sun\Java\Deployment\deployment.properties"
New-Item -ItemType File -Path $to -Force
Copy-Item $from $to
}
The code does not generate any meaningful data from the user directories and gives directory errors when running. The "New-Item" line was recently added as the directories were not being created. I'm a bit lost to be perfectly honest. I'm still new to syntax and what not sure what to do with this.
select-object -ExpandProperty Name
. I would suggest you run your script in the PowerShell ISE. Set breakpoints on various lines and examine the variables to see if the look like what you are expecting.foreach ($UserFolder in $Userfolders)
.