Solved

Powershell Syntax

Posted on 2013-12-04
5
414 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
  • 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
A brief introduction to what I consider to be the best editor for PowerShell.
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…
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…

825 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