Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I'm hoping for some assistance on this. I've been trying different variations of what I have for powershell code and I just can't get what I need to work. I would like my powershell script to run with Admin Rights because I also have to check or add registry keys.

What's I'm trying to do is copy an item from C:\Program Files\Mozilla Firefox\Plugins\plugin.xpi

and would like to get copied to these locations

C:\users\%users%\AppData\Roaming\Mozilla\Firefox\Profiles\*\extensions

The issue that I'm running into is that there are multiple profiles within the Profile parent directory in AppData and I would like to have the plugin copied to those profiles into the extensions subfolder. I'm hoping I'm making sense. Please let me know if I need to clarify anything.

What I have tried:

$source = 'C:\Program Files\Mozilla Firefox\plugins\plugin.xpi'
Get-ChildItem C:\Users | ForEach-Object {
Copy-Item -LiteralPath $source -Destination "$($_.FullName)\AppData\Roaming\Mozilla\Firefox\Profiles\*\extensions" -Verbose
}

I receive an error

"c\AppData\Roaming\Mozilla\Firefox\Profiles\*\extensions".
Copy-Item : Illegal characters in path.
At C:\pathtoscript\script.ps1:75 char:5
+ Copy-Item -LiteralPath $source -Destination "$($_.FullName)\AppDa ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Copy-Item], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.CopyItemCommand
Posted
Updated 24-Nov-20 23:46pm
v2

An extra Get-ChildItem / ForEach-Object should work, with an added Test-Path to exclude folders which don't exist:
PowerShell
$source = 'C:\Program Files\Mozilla Firefox\plugins\plugin.xpi'

Get-ChildItem "C:\Users" -Directory | Where-Object { Test-Path "$($_.FullName)\AppData\Roaming\Mozilla\Firefox\Profiles" } | ForEach-Object { Get-ChildItem "$($_.FullName)\AppData\Roaming\Mozilla\Firefox\Profiles" -Directory } | Where-Object { Test-Path "$($_.FullName)\extensions" } | ForEach-Object { Copy-Item $source -Destination "$($_.FullName)\extensions" -Verbose }
  • Get-ChildItem "C:\Users" -Directory lists the directories under the Users folder;
  • Where-Object { Test-Path "$($_.FullName)\AppData\Roaming\Mozilla\Firefox\Profiles" } filters to user folders with a Firefox profile;
  • ForEach-Object { Get-ChildItem "$($_.FullName)\AppData\Roaming\Mozilla\Firefox\Profiles" -Directory } lists the folders under each Firefox profile;
  • Where-Object { Test-Path "$($_.FullName)\extensions" } filters to profile folders with an "extensions" sub-folder;
  • ForEach-Object { Copy-Item $source -Destination "$($_.FullName)\extensions" -Verbose } copies the source to the extensions folder
 
Share this answer
 
Comments
Richard MacCutchan 25-Nov-20 6:26am    
+5. That was what I was trying to get to in my comment above, but my PowerShell skills are still at a somewhat basic level. I understand what $_.FullName means, but what is the reason for the $() surrounding it?
Richard Deeming 25-Nov-20 6:35am    
It lets you expand a command within an interpolated string. If you just did "$_.FullName\AppData", the string would contain "User1.FullName\AppData", where User1 was the name of the folder. By using $($_.FullName)\AppData", you get "C:\Users\User1\AppData" instead.
Powershell: Everything you wanted to know about variable substitution in strings[^]

In Powershell 7, it seems the default has changed slightly, so "$_\AppData" would return the full path (C:\Users\User1\AppData), and you could simplify those expressions.
GitHub - PowerShell/PowerShell: PowerShell for every system![^]
Richard MacCutchan 25-Nov-20 6:40am    
Thanks, I have been looking (not very hard) for a good tutorial site. The book I have is about a hundred years old.
Richard MacCutchan 25-Nov-20 6:42am    
I tried the open source version with VSCode but it made a bit of a mess of my views, so (blush) I reverted to cmd.exe. I really need to move with the times.
Richard Deeming 25-Nov-20 6:49am    
I've been using it with Windows Terminal, and it works quite well:
Windows Terminal 1.0 | Windows Command Line[^]

The PowerShell extension for VSCode still seems to be in preview:
PowerShell - Visual Studio Marketplace[^]
You cannot use an asterisk in a path like that, it has no valid meaning. You will need to create copy commands for each item in the list returned from Get-ChildItem.
 
Share this answer
 
Comments
[no name] 24-Nov-20 11:49am    
Thank you for the quick response Richard.

With what you said, how would I incorporate that into the code I have above?
Richard MacCutchan 24-Nov-20 12:22pm    
You need to get all the Child-Item entries below $_.FullName)\AppData\Roaming\Mozilla\Firefox\Profiles and use that list to build the destination address. So it is a For-Each on the list below C:\Users, and then another one on the profile list. Try building it up one part at a time and add the working portinons into a .ps1 script as you go.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900