Solved

adding credentials to a powershell command

Posted on 2014-12-31
2
105 Views
Last Modified: 2015-01-14
I am trying to delete some files on a network share.  I need to add different credentials in the script of an administrator to finish the command.
can I get the exact syntax of the powershell command if these are my variables.
these are made up examples of variables....

server unc path
\\apples\stems\*.*
domain = trees
username = moon
pwd = rock

I want to delete all files within the directory of \\apples\stems

also for bonus points... if you could tell me how to delete all the files in that directory that are over 14 days old

so two scripts requested.
Thanks!
0
Comment
Question by:jamesmetcalf74
2 Comments
 
LVL 24

Accepted Solution

by:
DMTechGrooup earned 500 total points
ID: 40525564
Why not run the script as a user with privileges

http://www.powershellmagazine.com/2012/11/09/pstip-start-powershell-as-a-different-user-with-elevated-privileges/

For files older than x days..

$limit = (Get-Date).AddDays(-15)
$path = "C:\Some\Path"

# Delete files older than the $limit.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force

# Delete any empty directories left behind after deleting the old files.
Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse

Open in new window

0
 
LVL 68

Expert Comment

by:Qlemo
ID: 40530250
The admin should have access to that folder, so why changing identity? It is difficult to connect to a share with a different user if there is a connection to any share on that server from this session, so it is something to avoid.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

867 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

18 Experts available now in Live!

Get 1:1 Help Now