0

I am looking to create a long script that does many things.

First the script will get a list of folders in a directory and output to a file I have this code below

Get-ChildItem -Path "filepath" | Select-Object Name | Out-File c:\dir.txt

The output of the file would look like

  • Name
  • -
  • 1234556 Description 1
  • 1244556 Description 2
  • 1234456 Description 3

I want to delete the first two lines then strip all the characters in the list except for the first 7 numbers and add the same text to the end of each directory name like this:

  • 1234556Descript
  • 1244556Descript
  • 1234456Descript

Then after that is done I want to create an AD group from this list of names.

1 Answer 1

1

You can do this without using a text file to store the file names in.

$Endding = "EnddingToGroup"
$path = "C:\FolderPath\"

Get-ChildItem -Path $path `
    | Select-Object -ExpandProperty Name `
    | %{ "$(($_).SubString(0,7))$Endding" } `
    | %{ New-ADGroup -Name $_ -GroupCategory Security -GroupScope Global -DisplayName $_ }

This will get a list of items in a folder, then takes the first 7 characters of the name of each item and add your '$endding' onto the end. Then it will make AD global security groups with the names.

If you want to see what group will be made before running it, just run the following part of the script without the New-ADGroup part:

$Endding = "EnddingToGroup"
$path = "C:\FolderPath\"

Get-ChildItem -Path $path | Select -ExpandProperty Name | %{ "$(($_).SubString(0,7))$Endding" }
Sign up to request clarification or add additional context in comments.

1 Comment

your code was confusing at first because the $path name had .txt in it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.