PowerShell Commande to do a subst

i need a commande to be able to do the following in PowerShell

Subst w: p:\w

but i need it to vérify if the letter is allready in use by a network share and if so unlink it firts
LVL 1
ntsabmanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Grasty86Commented:
I dont know about PowerShell, but it should be really easy with command prompt. SOmething like this should work

@echo off
net use t:
if errorlevel 2 (
      subst w: p:\w
) ELSE (
      net use w: /d
      subst w: p:\w
)
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Agree - no need to use PowerShell here. I would take a "brute force" approach by just trying to remove the net drive, ignoring any error, and then call subst:
@echo off
net use w: /D 2> nul
subst w: p:\w

Open in new window

BTW, the code in the prior comment needs to check for drive w: instead of t:, of course.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Grasty86Commented:
Right, when I was testing it I used T:, must have forgotten to switch that to W: before posting it.

Thanks for the correction Qlemo
0
ntsabmanAuthor Commented:
The brut force one is more stable and did not fail in testing the firts one fail from time to time witch is weard.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows 7

From novice to tech pro — start learning today.