Modify my little PowerShell script

Posted on 2014-03-11
Last Modified: 2014-03-11
Hi guys,

I have the following code, works butter smooth. Copies a few files/folders from source to destination then applies ACL's. See that 3rd "copy-item" which gets a .nk2 and copies it over? Well in the source there are MULIPLE .nk2 files (with different names), how can I get it to scan that \outlook folder and copy the biggest (file size) .nk2 item and rename is outlookprofile.nk2?

gc folderlist.txt | % {
$user = "$_"
Import-Csv "C:\TEMP\Favorites\input.csv" | ForEach {
If ($_.FQDN -eq $env:USERDNSDOMAIN) { $file_server = $_.'Server IP' } 
$FavAcl = Get-Acl -path \\$file_server\profiles$\$user\Favorites\
$SigAcl = Get-Acl -path "\\$file_server\profiles$\$user\Application Data\Microsoft\Signatures\"

if (Test-Path -Path "\\$file_server\profiles$\$user\Favorites")
copy-item -path  \\$file_server\profiles$\$user\Favorites -destination \\$file_server\userhome$\$user\Favorites -recurse -force 
copy-item -path  "\\$file_server\profiles$\$user\Application Data\Microsoft\Signatures" -destination \\$file_server\userhome$\$user\AppData\Roaming\Microsoft\Signatures\ -recurse -force -ErrorAction SilentlyContinue
copy-item -path  "\\$file_server\profiles$\$user\Application Data\Microsoft\Outlook\outlook.nk2" -destination \\$file_server\userhome$\$user\AppData\Roaming\Microsoft\Outlook\outlook.nk2  -ErrorAction SilentlyContinue
Get-ChildItem -path \\$file_server\userhome$\$user\Favorites -Recurse -Force | Set-Acl -AclObject $FavAcl
Get-ChildItem -path \\$file_server\userhome$\$user\AppData -Recurse -Force | Set-Acl -AclObject $SigAcl
Get-Acl -path \\$file_server\profiles$\$user\Favorites\ | Set-ACL -path \\$file_server\userhome$\$user\Favorites\
Get-Acl -path  "\\$file_server\profiles$\$user\Application Data\Microsoft\Signatures\" | Set-Acl \\$file_server\userhome$\$user\AppData\
remove-item -path \\$file_server\profiles$\$user -recurse -force

Open in new window

I'll need it to keep the ACL from source to destination as well.
Thanks guys.
Question by:levertm
  • 4
  • 3
LVL 34

Expert Comment

by:Dan Craciun
ID: 39920525
Replace line 13 with these 2 lines:
$bigNK2 = Get-ChildItem -path "\\$file_server\profiles$\$user\Application Data\Microsoft\Outlook" -Filter "*.nk2" | sort Length -Descending | select -First 1
copy-item -path  $bigNK2.FullName -destination \\$file_server\userhome$\$user\AppData\Roaming\Microsoft\Outlook\outlookprofile.nk2  -ErrorAction SilentlyContinue

Open in new window

The first will get the largest nk2 file in that folder and the second will copy it as outlookprofile.nk2 in the destination folder.


Author Comment

ID: 39920679
Hi Dan,

I''m getting an error:
Copy-item : Could not find a part of the path '\\10.x.x.x\userhome$\username\AppData\Roaming\Microsoft\Outlook\outlookprofile.nk2'.
Should it not be creating that folder if it doesnt exist? Which it may exist or may not exist.. What is strange is the \Signature folder doesnt exists either in the destination yet that copies without an errors.
LVL 34

Expert Comment

by:Dan Craciun
ID: 39920715
Yes, it should. If the script was running properly before, it should run the same now...

Can you add this between those 2 lines:
write-output $bigNK2.FullName

That's to see what is the name of the file being copied, maybe that's the problem.
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.


Author Comment

ID: 39920736
Two things:

1) The name of the file is shown and it's correct, I verified both path and file name and it does pick up the biggest file size in the source folder.

2) if the destination folder \outlook exists your additions work, the biggest .NK2 file gets copied and renamed to outlookprofile.nk2 in the destination folder. If the destination folder \outlook does not it exists, your additions do not work.

Strange huh.. yet the signature folder copies over without any hiccups, even if it does not exist.
LVL 34

Accepted Solution

Dan Craciun earned 500 total points
ID: 39920774
Just checked the manual and copy-item expect the path to exist if you copy a file.
If the source is a folder, it will create the path.

$bigNK2 = Get-ChildItem -path "\\$file_server\profiles$\$user\Application Data\Microsoft\Outlook" -Filter "*.nk2" | sort Length -Descending | select -First 1
if (!(Test-Path \\$file_server\userhome$\$user\AppData\Roaming\Microsoft\Outlook)) {
    New-Item \\$file_server\userhome$\$user\AppData\Roaming\Microsoft\Outlook -itemtype directory
copy-item -path  $bigNK2.FullName -destination \\$file_server\userhome$\$user\AppData\Roaming\Microsoft\Outlook\outlookprofile.nk2  -ErrorAction SilentlyContinue

Open in new window


Author Comment

ID: 39920853
That did it Dan, thank you very much.

LVL 34

Expert Comment

by:Dan Craciun
ID: 39920870
Glad I could help!

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
A brief introduction to what I consider to be the best editor for PowerShell.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now