Need a PS script

Welcome Forums General PowerShell Q&A Need a PS script

This topic contains 2 replies, has 3 voices, and was last updated by

 
Participant
6 months, 4 weeks ago.

  • Author
    Posts
  • #99325

    Inactive
    Points: 0
    Rank: Member

    Hello all. Very limited in my PS knowledge but I am confident that this can be done. I have a folder with many many sub directories.
    I need to find any occurrence of a directory named converted
    Move the files from within converted directory up one directory
    Delete the converted directory
    Delete files *.CAL from the directory that the converted files were moved into.

    Any help would be greatly appreciated as this will cut about 14 hours of manually doing this.

    Thanks,
    Chris

  • #99328

    Participant
    Points: 207
    Helping Hand
    Rank: Participant

    You can do many things with PoSH, this as well. Yet, this forum is not really a code writing service. The forum is here to help, where we can and within reason, with code you are having issues with.

    When you say limited knowledge, that really does not tell us where you really are, but that is different conversation.

    So, what have you already read up on regarding file and folder actions?
    Have you already looked at the online help for file and folder management. I.E:.,

    Get-ChildItem, Copy-Item, Move-Item, Remove-Item, Rename-Item, Test-Path, about_Loops, etc.?

    The normal file and folder actions is a normal thing in PoSH as well as any other scripting language.
    How, you go about your effort is your choice.

    There are lots of articles online PoSH and file and folder management.

    Example:

    PowerShell Basics: Managing Files and Folders
    'tomsitpro.com/articles/powershell-file-management,2-812.html'

    Getting Directory Sizes in PowerShell
    'blogs.technet.microsoft.com/heyscriptingguy/2012/05/25/getting-directory-sizes-in-powershell'

    File System Security PowerShell Module 4.2.3
    Allows a much easier management of permissions on files and folders using PowerShell
    'gallery.technet.microsoft.com/scriptcenter/1abd77a5-9c0b-4a2b-acef-90dbb2b84e85'

    Or just dive into the help files this way.

    # Get parameters, examples, full and Online help for a cmdlet or function
    
    # Get a list of all functions
    Get-Command -CommandType Function | 
    Out-GridView -PassThru -Title 'Available functions'
    
    # Get a list of all commandlets
    Get-Command -CommandType Cmdlet | 
    Out-GridView -PassThru -Title 'Available cmdlets'
    
    # Get a list of all functions for the specified name
    Get-Command -Name '*Item*' -CommandType Function | 
    Out-GridView -PassThru -Title 'Available named functions'
    
    # Get a list of all commandlets for the specified name
    Get-Command -Name '*Item*'  -CommandType Cmdlet | 
    Out-GridView -PassThru -Title 'Available named cmdlet'
    
    # get function / cmdlet details
    (Get-Command -Name Get-ChildItem).Parameters
    Get-help -Name Get-ChildItem -Examples
    Get-help -Name Get-ChildItem -Full
    Get-help -Name Get-ChildItem -Online
    
    Get-Help about_*
    Get-Help about_Functions
    
    # Find all cmdlets / functions with a target parameter
    Get-Command -CommandType Function | 
    Where-Object { $_.parameters.keys -match 'credential'} | 
    Out-GridView -PassThru -Title 'Available functions which has a specific parameter'
    
    Get-Command -CommandType Cmdlet | 
    Where-Object { $_.parameters.keys -match 'credential'} | 
    Out-GridView -PassThru -Title 'Available cmdlets which has a specific parameter'
    
    # Get namesd aliases 
    Get-Alias | 
    Out-GridView -PassThru -Title 'Available cmdlets which has a specific parameter'
    
    # Get cmdlet / function parameter aliases
    (Get-Command Get-ChildItem).Parameters.Values | 
    where aliases | 
    select Name, Aliases
    
    
    # All Help topics locations
    explorer "$pshome\$($Host.CurrentCulture.Name)"
    
    # Get any .NET types and their static methods from PowerShell. 
    # Enumerate all that are currently loaded into your AppDomain.
    #  
    [AppDomain]::CurrentDomain.GetAssemblies() | 
    foreach { $_.GetTypes() } | 
    foreach { $_.GetMethods() } | 
    where { $_.IsStatic } | 
    select DeclaringType, Name | 
    Out-GridView -PassThru -Title '.NET types and their static methods'
    

  • #99331

    Participant
    Points: 160
    Helping Hand
    Rank: Participant

    There are a few ways you could go.

    I would use Get-ChildItem to iterate through a filesystem to find these directories. Give it the highest level path you need, although I'd recommend avoiding trying to iterate through an entire disk if you can, and then supply the -Directory switch and the -Include "NAME" parameter to pick out the directories by name.

    Store these in a variable:

    $ConvertedFolders = Get-ChildItem ... (etc)

    Then, you can loop over the different folders using foreach ($folder in $ConvertedFolders) {} or $ConvertedFolders | ForEach-Object {} and make use of Split-Path to find the parent path of each Converted folder.

    Then use Copy-Item or Move-Item to move each item from the folders to the parent path (one level up). You can either use a loop or a wildcarded path to grab the items and move them. I would try with Copy-Item first and use the -WhatIf switch to make sure it's going to the right place before you actually have it perform the action.

    Then you can remove the extra files by calling Get-ChildItem and filtering on the file extension with -Include '*.ext'

The topic ‘Need a PS script’ is closed to new replies.