powershell script

Welcome Forums General PowerShell Q&A powershell script

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

 
Participant
1 year, 1 month ago.

  • Author
    Posts
  • #78622

    Participant
    Points: 0
    Rank: Member

    Hey!
    I want to write a script to find a directory in a path when I write the catalogs names and if the directory is there then copy it!

  • #78625

    Participant
    Points: 0
    Rank: Member

    Please show us what you have so far

  • #78628

    Participant
    Points: 0
    Rank: Member

    $foldername = Read-Host "Enter folder to check"

    get-childitem -path D:\inst\test -Directory -filter $folderName |
    foreach{
    write-host $_.FullName
    }

  • #78631

    Participant
    Points: 0
    Rank: Member

    so if this is your dir structure
    Folders
    folder1
    folder2
    folder3
    folder3a
    folder4

    and you wanted to search for folder3a it searches through the root dir folders and all its sub folders then copies it somewhere ?

  • #78637

    Participant
    Points: 0
    Rank: Member

    yes i want search for folder3a and then search a special folder(folder2) in folder3a then a special folder(folder1) in folder2 .....
    and kopy a folder to a special folder in 10 or 20 server!

  • #78647

    Participant
    Points: 0
    Rank: Member

    $serverlist1 = Get-Content “C:\Users\baa065sa\Powershell skript\Levtestservrar.txt”

    $foldername = Read-Host "Enter folder to check"
    get-childitem -path D:\Inst\test -Directory -filter $folderName |
    foreach{
    write-host Path exists on $_.FullName
    }

    $foldername = Read-Host "Enter folder to check"

    get-childitem -path D:\Inst\test -recurse -Directory -filter $folderName |
    foreach{
    write-host $_.FullName
    }

  • #78656

    Participant
    Points: 0
    Rank: Member

    Something like this ? Servers.txt has a list of server names in it

    $Servers = Get-Content "c:\temp\servers.txt"
    $Source = "C:\copied"
    $Destination = "c$\Temp"
    Foreach ($Server in $Servers)
    {Copy-Item $Source -Destination "\\$Server\$Destination" -Recurse}

  • #78671

    Participant
    Points: 0
    Rank: Member

    I'm not sure I understand your scenario, but I would suggest using the Test-Path cmdlet to verify if a specific path exists. You could use it something like the following:

    $SourceFolder = 'C:\Temp'
    $DestinationFolder = \\Server1\Data\
    $SourceFolderExists = Test-Path -Path $SourceFolder
    If($SourceFolderExists)
    {
      Copy-Item -Path $SourceFolder -Destination $DestinationFolder -Recurse
    }
    
    

    The above will copy the C:\Temp folder, and all of the content/folders/subfolders under it, to the specified destination folder. Hope that helps.

  • #78955

    Participant
    Points: 0
    Rank: Member

    it's good but the folder names come and change every time!
    I will write the names first and then it will check if the directory exists or not

    $foldername = Read-Host "Enter folder to check"
    get-childitem -path D:\CosmicInst\R8 -Directory -filter $folderName |
    if{
    write-host Path exists on $_.FullName
    }
    else{

    write-host Path not exists on $_.FullName
    }

  • #79003

    Participant
    Points: 0
    Rank: Member

    I want to write a script to check the folder for a server, and then another folder to check it in the folder, and if this folder exists, copy that folder to the other server from that server!
    For example, when we run the script, we will use the name of the first folder, then if there is a folder name that you have in that folder, and if there is one to copy it, if it does not exist, it says there is no folder.

The topic ‘powershell script’ is closed to new replies.