Solved

adding credentials to a powershell command

Posted on 2014-12-31
2
101 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 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

19 Experts available now in Live!

Get 1:1 Help Now