like in What is the difference between opening application by cmd/ps or by click? already discussed, I have the problem, that firefox does not use my bookmarks, that I copied with powershell, as long as I open/close firefox within a script.
The only chance I have is: open/close firefox manually and then overwrite bookmarks/passwords with powershell.
Also I created the profile with arguments, but as soon as firefox will be opened, firefox is creating a new profile on its own. Obviously it makes a difference, whether I open firefox by click or ps/cmd.
Does anybody has an idea, how to tell firefox which profile it shall use when the user opens firefox?
Here my code:
# Full path of the file
$appdata = $env:APPDATA
$folder = "$appdata\Custom"
$checkfile = "$folder\firstRunRestore"
#If the file does not exist, create it and execute statement.
if (-not(Test-Path -Path $checkfile -PathType Leaf)) {
try {
$null = New-Item -ItemType File -Path $checkfile -Force -ErrorAction Stop
#Create Firefox profile
$p = New-Object System.Diagnostics.Process
$p.StartInfo.UseShellExecute = $true;
$p.StartInfo.WorkingDirectory = "C:\Program Files\Mozilla Firefox";
$p.StartInfo.Filename = "firefox.exe";
$p.StartInfo.Arguments = "-CreateProfile $env:USERNAME";
$null = $p.Start()
Start-Sleep -s 10
Stop-Process -Name "Firefox"
# Wait to close all processes
Start-Sleep -s 7
# Restore files to profile
$firefox = "$env:APPDATA\Mozilla\Firefox\Profiles"
$curProfile = Get-ChildItem -Path $firefox | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$curPath = "$firefox\$curProfile"
$source = "X:\Desktop\FF-Profile"
$keydb = "key4.db"
$logins = "logins.json"
$places = "places.sqlite"
$favicons = "favicons.sqlite"
Copy-Item -Path "$source\$keydb" -Destination "$curPath" -Force
Copy-Item -Path "$source\$logins" -Destination "$curPath" -Force
Copy-Item -Path "$source\$places" -Destination "$curPath" -Force
Copy-Item -Path "$source\$favicons" -Destination "$curPath" -Force
Remove-Item $source -Force -Recurse
}
catch {
throw $_.Exception.Message
}
}
Related
I'm trying to achieve the following with this powershell script.
Copy any .zip file from folder dropfilehere to folder IN.
For each .zip file in folder "IN" open the zip file, find only the .csv file.
When .csv file is found, extract it to $dst under name DB.csv (overwrite old file).
Empty contents of folders "dropfilehere" and "IN"
Finally, when all the above is done, create a popup box with a message to the user using wscriptshell -
This is the issue. When the message is sent, the user gets 10+ popup boxes or an endless loop of them.
In the background i see cmd.exe and conhost.exe processes appearing as each popup box gets created.
I use a batch file to call the powershell script.
Powershell.exe -ExecutionPolicy Bypass -File C:\pathtoscript\call.ps1
exit
The script is:
$dst = "C:\Testing\DB"
Copy-item -Path "C:\Users\user\dropfilehere\*.zip" -destination "C:\Testing\Other\In" -Force
Foreach ($zipfile in (Get-ChildItem "C:\Testing\Other\In\*.zip" -Recurse)) {
Add-Type -Assembly System.IO.Compression.FileSystem
$zipFile = [IO.Compression.ZipFile]::OpenRead($zipfile)
$zipFile.Entries | where {$_.Name -like '*.csv'} | foreach {$FileName = $_.Name
[System.IO.Compression.ZipFileExtensions]::ExtractToFile($_, "$dst\DB.csv", $true)}
$zipFile.Dispose()
Remove-Item "C:\Testing\Other\In\*" -Recurse -Force
Remove-Item "C:\Users\user\dropfilehere\*" -Recurse -Force
$org="Name of Org"
$timeout = 60 # in seconds
$ws = New-Object -ComObject "Wscript.Shell"
$intButton = $ws.Popup("A new update message here`n
Another message here.",$timeout,$org, 0)
}
exit
There is code inside your foreach loop that should be placed after it, as shown below (properly indenting your code would have made that more obvious):
Add-Type -Assembly System.IO.Compression.FileSystem
$dst = "C:\Testing\DB"
Copy-item -Path "C:\Users\user\dropfilehere\*.zip" -destination "C:\Testing\Other\In" -Force
# Process all files.
foreach ($zipfile in (Get-ChildItem "C:\Testing\Other\In\*.zip" -Recurse)) {
$zipFile = [IO.Compression.ZipFile]::OpenRead($zipfile)
$zipFile.Entries |
Where-Object { $_.Name -like '*.csv' } |
ForEach-Object {
[System.IO.Compression.ZipFileExtensions]::ExtractToFile($_, "$dst\DB.csv", $true)
}
$zipFile.Dispose()
}
# Remove the folders containing the original *.zip files.
Remove-Item "C:\Testing\Other\In\*" -Recurse -Force
Remove-Item "C:\Users\user\dropfilehere\*" -Recurse -Force
# Show a message box.
$org = "Name of Org"
$timeout = 60 # in seconds
$ws = New-Object -ComObject "Wscript.Shell"
$intButton = $ws.Popup("A new update message here`nAnother message here.", $timeout, $org, 0)
I have Windows Server 2016 Datacenter (64 bit) as a File Server (contains several Shared folder & subfolders).
I want to make a list OR export user Folder Structure along with permissions ( Read, Modify, Full .. etc..)
I tried with below PS script but I am getting an error message with I have mentioned after the script.
Powershell
$FolderPath = dir -Directory -Path "E:\Project Folders\#Folder_Name" -Recurse -Force
$Report = #()
Foreach ($Folder in $FolderPath) {
$Acl = Get-Acl -Path $Folder.FullName
foreach ($Access in $acl.Access)
{
$Properties = [ordered]#{'FolderName'=$Folder.FullName;'AD Group or User'=$Access.IdentityReference;'Permissions'=$Access.FileSystemRights;'Inherited'=$Access.IsInherited}
$Report += New-Object -TypeName PSObject -Property $Properties
}
}
$Report | Export-Csv -path "C:\Folder Permissions\Folder Name.csv"
Error:
dir : Access to the path 'E:\Project Folders**Folder Path**\New folder' is denied. At C:\Users\Administrator\Documents\PS Script**File Name**.ps1:1 char:15 + ... olderPath = dir -Directory -Path "E:\Project Folders**Folder Name**" -Re ...+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (E:\Project Fold...ngar\New folder:String) [Get-Child Item], UnauthorizedAccessException + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand
Please help me out!
Thanks in Advance
As noted by the other comments.
This is not a PowerShell error/issue, it is a permissions one. The same thing can/will happen if you say you did this use case on the Windows folder tree.
Since you know this will happen, either fix the permissions on the tree you are working on or do this.
Get-ChildItem -Directory -Path 'C:\Windows\System32' -Recurse -ErrorAction SilentlyContinue
or if you want to just stop when a path fails.
# Treat non-terminating erros as terminating
$RootFolderUnc = 'C:\Windows\System32'
Try {Get-ChildItem -Directory -Path $RootFolderUnc -Recurse -ErrorAction Stop}
Catch [System.UnauthorizedAccessException]
{
Write-Warning -Message "$env:USERNAME. You do not have permissions to view this path."
$_.Exception.Message
}
I have created a script to delete the files of the users within the assigned path.
I'd like to unify the variables so I don't make it too long.
$folder = "C:\Users\$env:USERNAME\*"
$folder2 = "C:\Users\$env:USERNAME\Desktop\*"
$folder3 = "C:\Users\$env:USERNAME\Documents\*"
$folder4 = "C:\Users\$env:USERNAME\Contacts\*"
$folder5 = "C:\Users\$env:USERNAME\Downloads\*"
$folder6 = "C:\Users\$env:USERNAME\Favorites\*"
$folder7 = "C:\Users\$env:USERNAME\links\*"
$folder8 = "C:\Users\$env:USERNAME\Music\*"
$folder9 = "C:\Users\$env:USERNAME\OneDrive\*"
$folder10 = "C:\Users\$env:USERNAME\Pictures\*"
$folder11 = "C:\Users\$env:USERNAME\Searches\*"
$folder12 = "C:\Users\$env:USERNAME\Videos\*"
If (Test-Path $folder) {
Remove-Item $folder -Exclude AppData,Contacts,Desktop,Documents,Downloads,Favorites,Links,Music,OneDrive,Pictures,Searches,Videos -Force -ErrorAction SilentlyContinue
Remove-Item $folder2 -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item $folder3 -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item $folder4 -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item $folder5 -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item $folder6 -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item $folder7 -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item $folder8 -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item $folder9 -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item $folder10 -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item $folder11 -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item $folder12 -Recurse -Force -ErrorAction SilentlyContinue
}
I've tried the following way and it doesn't work:
$folder = "C:\Users\$env:USERNAME\*"
$folder2 = "C:\Users\$env:USERNAME\Desktop\*"
$folder3 = "C:\Users\$env:USERNAME\Documents\*"
$folder4 = "C:\Users\$env:USERNAME\Contacts\*"
$folder5 = "C:\Users\$env:USERNAME\Downloads\*"
$folder6 = "C:\Users\$env:USERNAME\Favorites\*"
$folder7 = "C:\Users\$env:USERNAME\links\*"
$folder8 = "C:\Users\$env:USERNAME\Music\*"
$folder9 = "C:\Users\$env:USERNAME\OneDrive\*"
$folder10 = "C:\Users\$env:USERNAME\Pictures\*"
$folder11 = "C:\Users\$env:USERNAME\Searches\*"
$folder12 = "C:\Users\$env:USERNAME\Videos\*"
If (Test-Path $folder) {
Remove-Item $folder -Exclude AppData,Contacts,Desktop,Documents,Downloads,Favorites,Links,Music,OneDrive,Pictures,Searches,Videos -Force -ErrorAction SilentlyContinue
Remove-Item $folder2 + $folder3 + $folder4 + $folder5 + $folder6 + $folder7 + $folder8 + $folder9 + $folder10 + $folder11 + $folder12 -Recurse -Force -ErrorAction SilentlyContinue
}
I would also like to get a log of what has been done, for example, if files or folders were found within the paths.
Thank you so much.
The -Path parameter of Remove-Item accepts a string array as input, if you want to remove more than one item at once.
Now with the syntax $folder2 + $folder3 you are creating a concatenated string like: "C:\Users\foo\Desktop\*C:\Users\foo\Desktop\*"". That is nothing that Remove-Item can handle.
You can build a string array of paths like this:
$folder = "C:\Users\$env:USERNAME"
$foldersToRemove = #(
"C:\Users\$env:USERNAME\Desktop",
"C:\Users\$env:USERNAME\Documents",
"C:\Users\$env:USERNAME\Contacts",
"C:\Users\$env:USERNAME\Downloads",
"C:\Users\$env:USERNAME\Favorites",
"C:\Users\$env:USERNAME\links",
"C:\Users\$env:USERNAME\Music",
"C:\Users\$env:USERNAME\OneDrive",
"C:\Users\$env:USERNAME\Pictures",
"C:\Users\$env:USERNAME\Searches",
"C:\Users\$env:USERNAME\Videos")
If (Test-Path $folder) {
Remove-Item $folder -Exclude AppData, Contacts, Desktop, Documents, Downloads, Favorites, Links, Music, OneDrive, Pictures, Searches, Videos -Force -ErrorAction SilentlyContinue
Remove-Item $foldersToRemove -Recurse -Force -ErrorAction SilentlyContinue
}
About your second requirement:
The -Verbose parameter will make Remove-Item tell you, which folders are removed. If you need your log in a file, you can redirect the verbose stream like this:
Remove-Item $foldersToRemove -Recurse -Force -ErrorAction SilentlyContinue -Verbose 4> .\deletedFolders.log
I am currently writing a script that takes a folder of files, moves the first file to a folder with a specific name, then move the rest to another folder with a number for a name.
My script works however it also moves the folder and renames it too. Which section of the code is causing this?
$path = "C:\Users\User1\Desktop\MergeTest\_First\"
$FileCount = Get-ChildItem -Path $path -File | Measure-Object | %{$_.Count}
$FirstFile = Get-ChildItem -Path $path -Force -File | Select-Object -First 1
$FinalReport = "C:\Users\User1\Desktop\MergeTest\___Final\TestOutput.xlsx"
Move-Item "C:\Users\User1\Desktop\MergeTest\_First\$FirstFile" $FinalReport
$Counter = 0;
Write-host $FileCount
for($Counter = 0; $Counter -lt $FileCount; $Counter++)
{
$FileInWork = Get-ChildItem -Path $path -Force -File | Select-Object -First 1
move-item "C:\Users\User1\Desktop\MergeTest\_First\$FileInWork" "C:\Users\User1\Desktop\MergeTest\__Second\$Counter.xlsx"
Write-host "File Moved"
}
What you could do is specify the -Include *.txt condition to your move-item commands so it is only to move just .txt, .log, or whatever file type you're moving and leave the folder how it is.
I believe your code could do with some cleaning up. Now you are executing Get-ChildItem 3 times, where using it once is enough.
Also, you should try and use the Join-Path rather than constructing the path and filenames yourself.
Especially where you do "C:\Users\User1\Desktop\MergeTest\_First\$FileInWork", you should realize that Get-ChildItem returns FileInfo and/or DirectoryInfo objects; not strings.
Anyway, the below code should do what you want:
# define the path where all other paths are in
$rootPath = "C:\Users\User1\Desktop\MergeTest"
# create the working paths using the common root folder path
$filesPath = Join-Path -Path $rootPath -ChildPath '_First'
$firstDestination = Join-Path -Path $rootPath -ChildPath '___Final'
$secondDestination = Join-Path -Path $rootPath -ChildPath '__Second'
# test if the destination folders exist and if not create them
if (!(Test-Path -Path $firstDestination -PathType Container)) {
Write-Host "Creating folder '$firstDestination'"
$null = New-Item -Path $firstDestination -ItemType Directory
}
if (!(Test-Path -Path $secondDestination -PathType Container)) {
Write-Host "Creating folder '$secondDestination'"
$null = New-Item -Path $secondDestination -ItemType Directory
}
# get an array of all FileInfo objects in $filesPath
# you could consider adding -Filter '*.xlsx' here..
$allFiles = Get-ChildItem -Path $filesPath -Force -File
Write-Host 'Total number of files found: {0}' -f $allFiles.Count
# move the files
for ($i = 0; $i -lt $allFiles.Count; $i++) {
if ($i -eq 0) {
# the first file should go in the $firstDestination folder with specified name
$target = Join-Path -Path $firstDestination -ChildPath 'TestOutput.xlsx'
}
else {
# all other files go to the $secondDestination folder
# each file should have the index number as name
$target = Join-Path -Path $secondDestination -ChildPath ('{0}.xlsx' -f ($i + 1))
}
$allFiles[$i] | Move-Item -Destination $target -Force -WhatIf
}
Hope that helps
Remove the -WhatIf if you are satisfied with whatever the output on console shows.
P.S. I really think you should edit your question and change its title, because nothing in the question has to do with Folder deleting after script ends..
I want to move all pictures from several folders to one destination folder, if they listed in my txt-file.
The script works, but there are about 81k pictures and 450k names (eg samlpe-green-bigpic-detail-3.jpg) in the txt-file, it is damn slow.
Is there a way to script it, so it works faster?
$qpath = "c:\sample\picz\"
$Loggit = "c:\sample\pic_move.log"
$txtZeileU = "c:\sample\names.txt"
$d_pic = "C:\sample\moved_picz"
$arrZeileU = Get-Content -Path $txtZeileU
foreach ($Zeile in $arrZeileU) {
Get-ChildItem -Path $qpath -Recurse |
where {$_.Name –eq $Zeile} |
Move-Item -Destination $d_pic -Verbose -Force *>&1 |
Out-File -FilePath $Loggit -Append
}