4

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.

5
  • Ok, so where are you getting stuck exactly? Please edit your question and post the code of what you've go so far, and explain which part(s) are not working as expected -- it'll help narrow the question down to something more than just another script request (which are off-topic). Nov 21, 2013 at 23:39
  • 1
    OK! Got an account and can comment back. Edits done.
    – Tim P.
    Nov 22, 2013 at 0:00
  • You probably want 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.
    – dangph
    Nov 22, 2013 at 6:32
  • You are also missing an opening brace "{" after foreach ($UserFolder in $Userfolders).
    – dangph
    Nov 22, 2013 at 7:05
  • With those two changes, it definitely does more. It currently writes to the Public and current user folder (when run from a PC). This will be pushed and run and LOCALSYSTEM and have full administrative rights to add the file to any user folder. However, how I can prevent it from adding to the accounts noted? How do I add the breakpoints noted to monitor the code? On a final note, I'd like to check the OS drive instead of assuming C:\ - I know I can also check OS architecture (XP, 7, etc.) but want to start slow! Thanks for the help dangph on getting it to work.
    – Tim P.
    Nov 22, 2013 at 16:56

1 Answer 1

3

This is just my thoughts and opinion on how I might do this...and I did change around the variables names a bit so they match up to the cmdlets used. You don't have to just made it more readable to me.


# I don't have a domain computer to work with so my local 
#  PC includes the "public" folder you likely want have 
#  this but nice to include just in case. As well the "Default*" 
#  is there because I'm on Windows 8.1 
#  so the upgrade modified the "Default" folder 
#  to be "Default.migrated" for some reason.

# The "-Exclude" allows you to put in the names of those items (directory names
#   or file names) that you want to exclude, it allows wildcard values as well)
#   Then as suggested in a comment using -ExpandProperty FullName allows the 
#   object to be passed as a string instead of a system object which
#   adds on extra characters that some other cmdlet may not like.
$userFolder = Get-ChildItem -Path 'C:\Users' -Exclude 'Default*','All Users','Administrator', 'Public' | 
   Select -ExpandProperty FullName

# just the file I was playing with here
$sFile = '.\text.txt'


foreach ($uf in $userFolder) {

    $dest = "$uf\AppData\LocalLow\Sun\Java\Deployment\"

# I want to test for the path to exist first and if it 
#  does then add the file, if it does not then you 
#  would create the directory and copy the file. 
#  I noticed in yours that you created an empty 
#  file and then copied it. Works the same way.

    if (Test-Path $dest) {
        Copy-Item -Path $sFile -Destination $dest -Force
    }
    else {
        New-Item -ItemType Directory -Path $dest -Force
        Copy-Item -Path $sFile -Destination $dest -Force
    }
}

As already noted I am on a standalone computer playing with this but the output of the commands I get are here:


New-Item -ItemType Directory -Path C:\Users\Shawn\AppData\LocalLow\Sun\Java\Deployment\ -Force
Copy-Item -Path .\text.txt -Destination C:\Users\Shawn\AppData\LocalLow\Sun\Java\Deployment\ -Force

You can also have it output the command it would execute if you want to test it first by just wrapping the commands in double-quotes: "Copy-Item -Path $sFile -Destination $dest -Force"

5
  • I'll give this a try tomorrow morning. Thanks for explaining what the commands do as well - that helps with learning how exactly they work which is a bonus for me.
    – Tim P.
    Nov 24, 2013 at 22:02
  • This worked great. I've since expanded on this a little for other requirements. One final question - is it possible to allow PowerShell permission to write to all user accounts? It traverses correctly, but cannot access other accounts at all (even though I am an administrator). Is there a way to force this to run as LOCAL_SYSTEM or be granted extra privileges?
    – Tim P.
    Nov 25, 2013 at 21:37
  • Are you executing the script through the administrator console or by Group Policy?
    – user94184
    Nov 25, 2013 at 21:45
  • For the time being this script is being packaged with the necessary files and executed via a batch file that calls powershell.exe as such: powershell.exe -ExecutionPolicy Bypass -File "Java Install.ps1" We only install Java on select machines in our network where necessary. For now this whole thing makes jobs easier for end users looking to install Java or for our work study students less familiar with internal Java settings.
    – Tim P.
    Nov 25, 2013 at 22:08
  • When calling it from the batch file follow this question, has a few options to choose from in order to get elevated privileged stackoverflow.com/questions/7690994/…
    – user94184
    Nov 25, 2013 at 23:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .