Solved

Powershell Syntax

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
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 tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…
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)

747 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

13 Experts available now in Live!

Get 1:1 Help Now