Jump to content

Creating a folder in each user profile - powershell help


Recommended Posts

I have been using the following powershell script with perfect results (filenames and paths changed):

 

$PathToMyFile = "MyFile.ext"
$UserProfiles = GWmi Win32_UserProfile -filter "LocalPath Like '%\\Users\\%'" | Select-Object -ExpandProperty LocalPath
ForEach ($Profile in $UserProfiles) {
    Copy-Item $PathToMyFile -Destination "$Profile\AppData\Roaming\folder\another_folder\data\stuff"

 

}

 

This copies a file to a specific location in each user profile on a machine.

 

I need to expand this a bit to first CREATE a folder\path in each profile where it doesn't already exist, then copy a file as shown above. My powershell skills are non-existent and my failed attempt looked like this:

 

$PathToMyFile = "MyFile.ext"

$UserProfiles = GWmi Win32_UserProfile -filter "LocalPath Like '%\\Users\\%'" | Select-Object -ExpandProperty LocalPath
ForEach ($Profile in $UserProfiles) {
New-Item "$Profile\AppData\folder\another_folder\stuff\data\config" ItemType directory
}
ForEach ($Profile in $UserProfiles) {
Copy-Item $PathToMyFile -Destination "$Profile\AppData\folder\another_folder\stuff\data\config"
}
 
Can someone please take pity on a newbie and help?
 
Of course if the software manufacturer would write their app so that it was profile-agnostic, that would help too!
Link to comment
Share on other sites


I'm a little busy right now, (will return later), but just wanted to let you know that your method of identifying profile folder locations whilst possibly good for the majority of situations may not be robust enough for all scenarios.

 

BTW Get-ChildItem accepts wildcards so you could quite easily have achieved the same as your working code like this:

 
#Using Powershell 2.0
$MyFile = 'MyFile.ext'$MyDest = '\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup'GCI -Path ('C:\Users\*\' + $MyDest) | Where {$_.PSISContainer} | % {Cp -Path $MyFile -Destination $_.FullName}

#Using Powershell 3.0

$MyFile = 'MyFile.ext'$MyDest = '\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup'GCI -Path ('C:\Users\*\' + $MyDest) -Directory | % {Cp -Path $MyFile -Destination $_.FullName}
Link to comment
Share on other sites

Thank you...the first portion is working for the specific need I had (a quick means to a quickly needed and specific end). I'm all for improving things, believe me, but my immediate need is copying a file to all known profiles to a path that does not yet exist...in other words create a folder/path in all profiles, then use the existing PS copy that I know works.

 

Looking forward to seeing the rest of the improved script and thanks very much again!

Link to comment
Share on other sites

See if this suits your task; (it uses the same method of only looking at the C:\Users path for profile directories)

$MyFile = 'MyFile.ext'$MyDest = '\AppData\Roaming\folder\another_folder\data\stuff\'ForEach ($User In (GCI "C:\Users" -Exclude Public,Default*)) {    $Null = NI -I D -F -Path "C:\Users\$($user.Name)$MyDest"    Cp -Path $MyFile -Destination "C:\Users\$($user.Name)$MyDest$MyFile"}

If you really do want just a fix for your script then try this;

$PathToMyFile = "MyFile.ext"$UserProfiles = GWmi Win32_UserProfile -filter "LocalPath Like '%\\Users\\%'" | Select-Object -ExpandProperty LocalPathForEach ($Profile in $UserProfiles) {    New-Item "$Profile\AppData\folder\another_folder\stuff\data\config" -ItemType directory    Copy-Item $PathToMyFile -Destination "$Profile\AppData\folder\another_folder\stuff\data\config"}

To improve the robustness that I mentioned in my opening post then you could use this instead;

$PathToMyFile = "MyFile.ext"$UserProfiles = GWmi Win32_UserProfile -Filter "Special != True" | Select-Object -ExpandProperty LocalPathForEach ($Profile In $UserProfiles) {    New-Item "$Profile\AppData\folder\another_folder\stuff\data\config" -ItemType Directory | Out-Null    Copy-Item $PathToMyFile -Destination "$Profile\AppData\folder\another_folder\stuff\data\config"}

To incorporate that robustness to my example at the head of this post then;

$MyFile = 'MyFile.ext'$MyDest = '\AppData\Roaming\folder\another_folder\data\stuff\'ForEach ($User In (GWmi Win32_UserProfile -F "Special != True" | Select -Expand LocalPath)) {$Null = NI -I D -F -Path "$($User)$MyDest"    Cp -Path $MyFile -Destination "$($User)$MyDest$MyFile"}
Link to comment
Share on other sites

  • 1 month later...

WOOHOO! Thanks VERY much and sorry for the late reply. I've been trying all the variations and things are working perfectly.

 

It looks like all I missed on my initial try was a hyphen in front of "ItemType Directory".

 

I still wish our vendor would write stuff to C:\ProgramData to make it profile-agnostic. Besides, who really shares machines any more?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...