• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Recursive issue in Powershell

Maglio

Weaksauce
Joined
Feb 15, 2008
Messages
64
i'm pretty new to powershell, and haven't been able to figure out a solution to the following problem.

the company i work for need to retain files from terminated workers for a certain amount of time. i am trying to automate the task through a powershell script. for the most part i have it figured out how to pull only the files i want from the folders i want.

the issue i have is with the desktop folder. i need it to pull all information whether it is on the desktop or in a folder that is on the desktop. the problem comes in with the copy-item -recurse function. if there are no extra folders on the desktop, it will error. if i don't include the recurse option, it will only pull in the data on the desktop and no data from the subfolders.

so my question is this...using the powershell script, how would i check for subfolders and then if subfolders exist, run the command with the recurse option, else run it without.

if there is anything i'm missing, please let me know. like i said, i'm new to powershell and am still trying to figure it out.

Mag
 
How about something like this at the top of your script? (update $yourObject)

trap [System.Exception]
{
write-host ("Exception caught: " + $yourObject.Exception.GetType().Name);
continue;
}
 
actually, just got it figured out. you can actually do if/then statement.

here is what i found works for me:
if (Get-ChildItem $source -force | Where-Object {$_.mode -match "d"}) {Get-ChildItem $source -Recurse -Exclude $exclude | Copy-Item -Destination {Join-Path $fnlDest $_.FullName.Substring($source.length)}}
else {Copy-Item -Recurse $source $fnlDest}

hope that helps anyone else looking for a similar situation
 
Back
Top