none
Powershell - Create a folder from a file name in a CSV file, then move that file into that desination RRS feed

  • Question

  • I have 6000 PST files that need to be moved from a source location to a server share.  Where my PowerShell is good enough to achieve this, if I had to move all the files in a given location, its not strong enough to figure out a way to do this when the PST files requiring the move are only 6000 out of 20000 in a complex folder structure.

    I have a CSV file containing all the file paths of the PST files needing to be moved, and a single location to move them to, and again I would be able to script this, however, the requirement for my client is that the destination folder needs to be the name of the file that's moving.

    So, I need help with something like this.

    Source CSV file lists files as sample below.
    D:\PST\Folder2\BobSmith.PST
    D:\PST\Folder2\TedSmith.PST
    D:\PST\Folder2\Folder3\GregSmith.PST

    Destination result would need to be

    \\Server\Share\BobSmith\BobSmith.PST
    \\Server\Share\TedSmith\TedSmith.PST
    \\Server\Share\GregSmith\GregSmith.PST

    I would like to be able to leverage PowerShell to query my CSV file with the 6000 files listed within it (using a column header or not, which ever is easier), have it create a new folder per file, and move the file from the source location specified in the CSV to the central Server Share.

    So far I have come up with the following code, but it can only manage to do all items in a given location.

    Get-ChildItem -File |  # Get files
     Group-Object { $_.Name -replace '_.*' } |  # Group by part before first underscore
      
      
      ForEach-Object {
        # Create directory
        $dir = New-Item -ItemType directory -Path \\Server\Share\ -Name $_.Name
        # Move files there
        $_.Group | Move-Item -Destination $dir
      }

    I can't seem  to figure out how to replace "Get-ChildItem -File" with a import-CSV (for example).

    Any help or pointers are fully appreciated.

    Thanks
    Rich

    • Moved by Bill_Stewart Friday, March 15, 2019 7:09 PM This is not "scripts on demand"
    Tuesday, February 5, 2019 5:20 PM

All replies

  • Start by reading how to use a CSV file.

    help import-csv -online

    All of your question would be resolved if you learned basic PowerShell:

    Microsoft Virtual Academy - Getting Started with Microsoft PowerShell


    \_(ツ)_/

    Tuesday, February 5, 2019 6:53 PM
  • Amazeballs !

    Totally why I hardly post here are dickhead answers like yours.

    Wednesday, February 6, 2019 9:11 AM
  • Have you tried importing your CSV or do you just want someone to do everything for you?

    You will also need to learn how to use "split" to get the name to use.

    Without doing the basic tutorial all of this will be very confusing for you.  Start by reading how to use the CS and create a loop.  Next add the split.

    Of course the file example you posted is not a CSV.  That could allow you to just use Get-Content.

    To split a file name:

    (('D:\PST\Folder2\BobSmith.PST' -split '\\')[3] -split '\.')[0]

    Or you can use  a RegEx to extract the name.


    \_(ツ)_/

    Wednesday, February 6, 2019 9:18 AM
  • No, I don't want you to do EVERYTHING for me. But being a dick doesn't help me does it ?
    Wednesday, February 6, 2019 4:07 PM
  • Then you need to ask a specific question and stop trying to insult your way to an answer.  What is it that you can't do or understand.


    \_(ツ)_/

    Wednesday, February 6, 2019 5:04 PM