Solved

Powershell Syntax

Posted on 2013-12-04
5
422 Views
Last Modified: 2013-12-06
Hello.  I hate to post this here as I'm sure it's simple and have been searching, but I am finding how to create a simple directory using PowerShell syntax.

My issue, getting the syntax right to append a new directory to the end of 26 directories that are all the same, but the end folder varies as it's done by state, and there is a folder within.  So essentially I need a wildcard path and then be able to append a new directory say "Doctest" at the end of each line, each directory is basically:

c:\test\vtest\az
c:\test, vtest\mo
c:\test\vtest\hi,

for example.  

So that is the most symplistic.  The other possibility, the directories will be like :

c:\test\vtest\az\result
c:\test, vtest\mo\result
c:\test\vtest\result.  


But I really want it to be
c:\test\vtest\az\result\Doctest
c:\test, vtest\mo\result\Doctest
c:\test\vtest\result\Doctest


I am trying to use PowerShell for this and I am very, very new to PowerShell and would like to script this task (as a good place to start) rather than create these folders manually.

Thoughts?  Thanks.
0
Comment
Question by:jnordeng
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 385 total points
ID: 39697237
If you are sure about the end directory name is "Result" then you can use this code.. It will search all folders inside the C:\temp and if the end folder name is Result then it will create a folder inside it.
$Dir = Get-ChildItem C:\temp -Recurse | 
 ?{$_.PSIsContainer -and $_.Fullname -match "Result$"} |
	Select -ExpandProperty Fullname
$Dir | % {
	New-Item -ItemType Container -Path $_ -Name "Doctest" -Force
}

Open in new window

If you are unsure about the end directory name the try this.. It will search all folders inside the C:\temp to find all end folder name and then create a folder inside it.
$Dir = Get-ChildItem "C:\Temp" -Recurse | 
  ? {$_.PSIsContainer -and `
  !(Get-ChildItem $_.Fullname | ?{$_.PSIsContainer})} |
	Select -ExpandProperty Fullname
$Dir | % {
New-Item -ItemType Container -Path $_ -Name "Doctest"
}

Open in new window

0
 

Author Comment

by:jnordeng
ID: 39697370
Awesome, thank you.  I will give this a try in the morning.  Appreciate the help.
0
 

Author Comment

by:jnordeng
ID: 39699299
Works well, so rather than modify and insert within the path is there a way (syntax) to plop on the end if the rest of the path is the same but the second to last is different?  Thanks.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39701658
Second code should work that way right? if not can you provide an example of the directory structure?
0
 

Author Closing Comment

by:jnordeng
ID: 39701717
Thanks for your help, did the trick, now to come up with other things to try and start working on PowerShell Scripting. ;)
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Resolving an irritating Remote Desktop connection that stops your saved credentials from being used.
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

729 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